# Optional arguments in Julia v0.6

**URL:** <https://discourse.julialang.org/t/optional-arguments-in-julia-v0-6/4630>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 3, 2017, 8:38pm UTC](https://discourse.julialang.org/t/optional-arguments-in-julia-v0-6/4630 "2017-07-03T20:38:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 3, 2017, 8:38pm UTC](https://discourse.julialang.org/t/optional-arguments-in-julia-v0-6/4630/1 "2017-07-03T20:38:30Z")

</div>

Is there any trick to handle optional arguments in Julia? Sometimes I want to setup some of the arguments of a function for the user, however; this setup is not necessarily straightforward that I could just pass a default value in a keyword argument. Consider the function below:

```julia
function f(; b=nothing)
  if b != nothing
    # assertions to make sure b is of the right type
  end
  println(b)
end

```

If the user specifies `b`, I need to assert its type, but it seems that I am reinventing the wheel here. There is probably a better way of handling this situation? I was thinking of something like `Nullable` in Julia v0.6, but I am not very familiar with it. In C++, I would use [Boost.Optional](http://www.boost.org/doc/libs/1_60_0/libs/optional/doc/html/index.html).

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [July 3, 2017, 8:56pm UTC](https://discourse.julialang.org/t/optional-arguments-in-julia-v0-6/4630/2 "2017-07-03T20:56:26Z")

</div>

You can just do `b::Union{T, Void}`, with `T` the type you would like to assert.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [July 4, 2017, 2:03am UTC](https://discourse.julialang.org/t/optional-arguments-in-julia-v0-6/4630/3 "2017-07-04T02:03:01Z")

</div>

If speed is a concern on v0.6, you might also consider using `b::Nullable{T} = Nullable{T}()`. I think this the same as an optional in C++.

(Note: the `Union` approach will be fast on v0.7/v1.0)

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 4, 2017, 2:13am UTC](https://discourse.julialang.org/t/optional-arguments-in-julia-v0-6/4630/4 "2017-07-04T02:13:41Z")

</div>

Thank you all, could you please elaborate on the differences between `Union{T,Void}` and `Nullable{T}`? What is the advantage/disadvantage of each approach when modeling optional arguments?

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [July 4, 2017, 4:07am UTC](https://discourse.julialang.org/t/optional-arguments-in-julia-v0-6/4630/5 "2017-07-04T04:07:07Z")

</div>

`b::Nullable{T}` is a container - you will need to check if it contains data with `isnull(b)` and then use `get` (or `getindex`) to extract the data of type `T`. Also, the code calling the function may need to explicitly wrap the data in a `Nullable{T}`…

`b::Union{T, Void}` will be an “unboxed” `T` or else a `nothing`. You can check if `b === nothing` (or `b isa Void`) and if not you can assume that `b` _is_ the data (of type `T`) you want. Your code example in the OP more-or-less does this already (maybe you want `!==` rather than `!=` to help inference? not sure…)

Really, it’s just a difference in style. In a static language like C++ the container is quite necessary. In a dynamic language like Julia, both options are available and arguably the `Union` approach is somewhat simpler to work with. The upcoming `Union` performance improvements (as fast as `Nullable`) will mean that `Union`s will probably be the preferred approach in the future - but both options will remain open to you. If your function isn’t performance critical (say, called in an inner loop of a numeric algorithm), feel free to use the `Union` approach now - it won’t slow you down by _that_ much, really (and inference on v0.6 seems good enough that type instabilities won’t propagate).

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 4, 2017, 4:16am UTC](https://discourse.julialang.org/t/optional-arguments-in-julia-v0-6/4630/6 "2017-07-04T04:16:03Z")

</div>

Thank you for the very clear explanation @andyferris 👍 I personally like the `Union` type more and the fact that by using it instead of `Nullable` we don’t need to wrap the input in the call site.
