In R you can use NULL arguments in functions like myfun = function (a=NULL, b, c) can you do this in Julia? I'm asking because I'd like variable a as a condition : if a = NULL do this, else do that. I can just write different functions of course but they would mostly repeat each other. I can also just assign arbitrary numbers but it seems using NULL is more clear. Thanks!.
Asked
Active
Viewed 1,178 times
6
-
Perhaps you need `Nullable{Int64}()` if it is a integer64 – akrun Dec 04 '17 at 08:00
-
1what from the documentation wasn't clear? https://docs.julialang.org/en/stable/manual/functions/ -and- https://stackoverflow.com/questions/37751541/none-value-in-julia -and- https://stackoverflow.com/questions/42499528/julia-convention-for-optional-arguments – MichaelChirico Dec 04 '17 at 08:03
-
2Possible duplicate of [Julia convention for optional arguments](https://stackoverflow.com/questions/42499528/julia-convention-for-optional-arguments) – Gnimuc Dec 04 '17 at 12:35
-
The equivalent of `NULL` is `nothing`, of type `Nothing`. If you can, it's better to avoid it in function arguments, but at times it's fine. It's a subtle issue. – PatrickT Jun 13 '21 at 06:16
1 Answers
7
One of the most common questions from people coming to a new language is how to code exactly like in the language they come from. But testing for NULL is not likely to be the best way to implement this in Julia. Mostly you'll define two methods for your function:
function myfun(a, b, c)
...
end
function myfun(b, c)
...
end
With the different behaviours.

Michael K. Borregaard
- 7,864
- 1
- 28
- 35