https://github.com/helgee/optionaldata.jl
Work with global data that might not be available
https://github.com/helgee/optionaldata.jl
Last synced: 3 months ago
JSON representation
Work with global data that might not be available
- Host: GitHub
- URL: https://github.com/helgee/optionaldata.jl
- Owner: helgee
- License: other
- Created: 2017-03-21T11:48:57.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-04-29T13:35:11.000Z (about 5 years ago)
- Last Synced: 2025-02-14T01:17:53.280Z (over 1 year ago)
- Language: Julia
- Homepage:
- Size: 17.6 KB
- Stars: 6
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# OptionalData
*Work with global data that might not be available.*
[](https://github.com/helgee/OptionalData.jl/actions)
[](https://codecov.io/gh/helgee/OptionalData.jl)
This package provides the `@OptionalData` macro and the corresponding `OptData`
type which is a thin wrapper around a nullable value (of type `Union{T, Nothing} where T`).
It allows you to load and access globally available data at runtime in a type-stable way.
## Installation
The package can be installed through Julia's package manager:
```julia
Pkg.add("OptionalData")
```
## Usage
*OptionalData* has the following use cases:
1. Parts of your package depend on data from the internet while other parts do not.
In the case of a network outage the package should offer a degraded experience but
the independent parts should still function.
2. Your package requires manual initialisation steps, e.g. loading data from a
user-supplied file, and you do not want to repeat yourself writing code that
checks for the availability of the data.
You declare optional global data with the `@OptionalData` macro:
```julia
using OptionalData
# @OptionalData name type [error_msg]
@OptionalData OPT_FLOAT Float64 "Forgot to load it?"
# this expands to
const OPT_FLOAT = OptData{Float64}(string(:OPT_FLOAT), "Forgot to load it?")
```
You access its value with `get` and check whether it is available with `isavailable`:
```julia
# This will throw a `NoDataError` because OPT_FLOAT does not contain a value, yet.
get(OPT_FLOAT)
# ERROR: OPT_FLOAT is not available. Forgot to load it?
isavailable(OPT_FLOAT) == false
```
Use `push!` to load the data:
```julia
push!(OPT_FLOAT, 3.0)
isavailable(OPT_FLOAT) == true
get(OPT_FLOAT) == 3.0
```