# Optional argument is a struct

**URL:** <https://discourse.julialang.org/t/optional-argument-is-a-struct/64502>\
**Category:** New to Julia\
**Created:** [July 12, 2021, 11:51am UTC](https://discourse.julialang.org/t/optional-argument-is-a-struct/64502 "2021-07-12T11:51:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [July 12, 2021, 11:51am UTC](https://discourse.julialang.org/t/optional-argument-is-a-struct/64502/1 "2021-07-12T11:51:44Z")

</div>

Hello,  
I have a function that takes say 3 arguments `fun(a,b,c)`, where they are all structs I defined and the third one is optional.  
If the third one is present, then I’ll need to call `fun2`.  
What’s a clean way to do this? I could simply do

```julia
fun(a,b,c=nothing)
    if !c==nothing
        fun2()
    end
...

```

But that would be type unstable, right? I could also have `c` be a struct with default values, maybe one of them being `exists` and defaulting to `true` and do

```julia
fun(a,b,c=Struct3(defined=false))
    if c.defined
        fun2()
    end
...

```

But that forces me to set defaults to `c`, which I’d rather not have.  
Any ideas?  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [July 12, 2021, 12:05pm UTC](https://discourse.julialang.org/t/optional-argument-is-a-struct/64502/2 "2021-07-12T12:05:06Z")

</div>

Define your `fun2` as a method of fun:

```julia
fun(a, b, c) = ...
fun(a, b) = .... # this is your fun2

```

That is what Julia is all about: multiple dispatch!

---

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [July 12, 2021, 12:43pm UTC](https://discourse.julialang.org/t/optional-argument-is-a-struct/64502/3 "2021-07-12T12:43:43Z")

</div>

Yep, that would work, but `fun` is actually a pretty long function that I’d rather not duplicate.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [July 12, 2021, 12:50pm UTC](https://discourse.julialang.org/t/optional-argument-is-a-struct/64502/4 "2021-07-12T12:50:57Z")

</div>

Can you not factor out what is needed by both methods into, say, `_fun`, and then call that from both methods?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 12, 2021, 12:56pm UTC](https://discourse.julialang.org/t/optional-argument-is-a-struct/64502/5 "2021-07-12T12:56:08Z")

</div>

> [@Ribeiro](#):
>
> But that would be type unstable, right?

No, it’s totally fine. Just make sure you use `===` or `!==` (its opposite) for checking for `nothing`. Only one possible value is `=== nothing`, and that’s `nothing` itself, so the compiler will be able to figure out which branch of the `if` statement is taken just from the type of `c` alone (by checking if that type is `Nothing` or something else).

Having an optional argument default to `nothing` and then checking `if c !== nothing` in the body of the function is a totally reasonable thing to do in Julia.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [July 12, 2021, 12:59pm UTC](https://discourse.julialang.org/t/optional-argument-is-a-struct/64502/6 "2021-07-12T12:59:48Z")

</div>

I’ll share some advice I was given.

1. Define a function `_fun` that does the bulk of the work. Then have methods `fun(a, b, c)` and `fun(a, b)` both call `_fun` internally but with different pre- and post-processing above and below respectively.
2. You can use a distinct `struct` rather than `nothing` to define methods for your different cases: [Application Code Organization - #15 by lmiq](https://discourse.julialang.org/t/application-code-organization/53224/15).

---

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [July 12, 2021, 3:33pm UTC](https://discourse.julialang.org/t/optional-argument-is-a-struct/64502/7 "2021-07-12T15:33:19Z")

</div>

Thanks, folks. I’ll stick to the `nothing` approach for now.
