Your first example definition uses a optional positional argument and not a keyword argument. Your second example definition uses a keyword argument named tl
. In Julia, when you call a function with a keyword argument you must use this name, as you discovered (either g(x; tl=y)
or g(x; tl)
which is just a shorthand for g(x; tl=tl)
.
Now, to the question about defaults: A common way is to use nothing
as a default, and check for that inside the function body, for example:
function g(ti::AbstractVector; tl = nothing) # or tl::Union{Timelimits,Nothing} = nothing
if tl === nothing
tl = Timelimits(ti[begin],ti[end])
end
# ...
end