I am running julia 1.0 on Manjaro Linux. I have created a macro like this, which can generate a power function, with optionally a constant offset and/or a constant factor:
macro pow(functionname::Symbol, pow::Int, add::Union{Nothing,Number}=nothing, fact::Union{Nothing,Number}=nothing)
expr = :(x ^ $pow)
if !(fact === nothing)
expr = :($fact * $expr)
end
if !(add === nothing)
expr = :($expr + $add)
end
return quote
$(esc(functionname))(x) = $expr
end
end
This works pretty well and I can call it either like this: @pow f 2
, this: @pow f 2 1
or like this: @pow f 2 1 .5
, which does exactly what it should.
Now, I want the macro to generate a function without an offset but with a constant factor and I don’t want to just set the offset to 0, because it would still generate the extra code. I tried just calling it like this: @pow f 2 nothing .5
, but it fails with this error:
ERROR: LoadError: MethodError: no method matching @pow(::LineNumberNode, ::Module, ::Symbol, ::Int64, ::Symbol, ::Float64)
Closest candidates are:
@pow(::LineNumberNode, ::Module, ::Symbol, ::Int64, ::Union{Nothing, Number}, ::Union{Nothing, Number}) at /home/simeon/macro.jl:2
@pow(::LineNumberNode, ::Module, ::Symbol, ::Int64) at /home/simeon/macro.jl:2
@pow(::LineNumberNode, ::Module, ::Symbol, ::Int64, ::Union{Nothing, Number}) at /home/simeon/macro.jl:2
in expression starting at REPL[2]:1
, which seems to indicate that it interprets nothing
as a Symbol and not a Nothing
-Type.
Is this behavior actually intentional? If so, is there a way to tell Julia not to interpret nothing
as a symbol but as a value? If also tried it like this: $nothing
, nothing::Union{Nothing,Number}
and even with a function that just returns nothing
, all of which failed. Any help is appreciated.