# Mark a function argument of any type as optional by generating a Union. # If a default value is not defined, it assumes "nothing". # # Examples: # function(x::Int, @maybe y::MyType) # function(x::Int, @maybe y::MyType=someval) macro maybe(argexpr) default = :nothing if argexpr.head == :(=) argexpr, default = argexpr.args end @assert (argexpr.head == :(::)) "@maybe is for typed function arguments." name, ptype = argexpr.args deftype = typeof(eval(default)) Expr(:kw, :($name::Union($deftype, $ptype)), default) end