The latter is the idiomatic/Julian solution: assign kwarg=nothing
in the signature, and in the function body, and set kwarg
to its default value if isnothing(kwarg)
. nothing
serves as an “unset” sentinel value that doesn’t special case a regular value (e.g. using -1
as the “unset” value). nothing
could even still be used as a valid value by giving the kwarg
as Some(nothing)
.
function A(x; kwarg=nothing)
if isnothing(kwarg)
kwarg = 100
end
# rest of function
end