Work with global data that might not be available
Author helgee
5 Stars
Updated Last
2 Years Ago
Started In
March 2017


Work with global data that might not be available.

Build Status Coverage

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.


The package can be installed through Julia's package manager:



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:

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:

# This will throw a `NoDataError` because OPT_FLOAT does not contain a value, yet.
# ERROR: OPT_FLOAT is not available. Forgot to load it?
isavailable(OPT_FLOAT) == false

Use push! to load the data:

push!(OPT_FLOAT, 3.0)
isavailable(OPT_FLOAT) == true
get(OPT_FLOAT) == 3.0

Required Packages

No packages found.