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
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
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!
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.
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.