# How to handle "optional data"?

**URL:** <https://discourse.julialang.org/t/how-to-handle-optional-data/2604>\
**Category:** General Usage\
**Created:** [March 11, 2017, 7:21am UTC](https://discourse.julialang.org/t/how-to-handle-optional-data/2604 "2017-03-11T07:21:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [March 11, 2017, 7:21am UTC](https://discourse.julialang.org/t/how-to-handle-optional-data/2604/1 "2017-03-11T07:21:06Z")

</div>

Hi everyone,

Let’s say I have a package that provides some functions. Of these functions only a few depend on some global data downloaded from the internet. The package should be able to download the data automatically but should not force this on the user (e.g. calling it in ` __init__ `) because they might already have the data elsewhere. Also the rest of the package should work even if the data is not available.

**The question is what would the type of this “optional data” be?**

```julia
module SomeModule

# What am I?
const data = ?

# I don't need the data
f(x) = 1.0x

# I don't need the data
g(x) = 2.0x

# I need the data and will error if it's not here
h(x) = data*x

end

```

I have experimented with several solutions and would like to know which one you would prefer.

1. `Nullable`

2. Wrap `Nullable` in a mutable type

3. Just use a `Ref`

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [March 11, 2017, 10:24am UTC](https://discourse.julialang.org/t/how-to-handle-optional-data/2604/2 "2017-03-11T10:24:41Z")

</div>

I would prefer the `Ref` solution. Alternatively wrapping the `Nullable` in a `Ref` gves you the same as `2.`

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [March 11, 2017, 4:11pm UTC](https://discourse.julialang.org/t/how-to-handle-optional-data/2604/3 "2017-03-11T16:11:53Z")

</div>

Why do you want to declare `data` as “constant” when it is not?  
`data = Nullable{TypeOfData}()` will work fine, as long as you don’t prefix it with `const`.

Put a type assertion in `h()` to get efficient code.

```julia
h(x) = get(data::Nullable{TypeOfData})*x

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 11, 2017, 4:40pm UTC](https://discourse.julialang.org/t/how-to-handle-optional-data/2604/4 "2017-03-11T16:40:21Z")

</div>

A trick I have used several times, which may not be ideomatic/efficient is to define a global `const data = TypeOfData[]`. You can then check `isempty(data)` and work on `data[1]`.

---

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [April 4, 2017, 6:17am UTC](https://discourse.julialang.org/t/how-to-handle-optional-data/2604/5 "2017-04-04T06:17:20Z")

</div>

While I initially used the `Ref` pattern I repeated myself quite often. So in the end I used approach 2 and turned it into a package: [https://github.com/helgee/OptionalData.jl](https://github.com/helgee/OptionalData.jl)
