You could do something like the following macro, choosing a value at expansion time, depending on a statically known Symbol
:
julia> macro floop(selector::QuoteNode)
choices = Dict(:a => 5, :b => 10)
ex = :(1 + $(choices[selector.value]))
return ex
end
@floop (macro with 1 method)
julia> @macroexpand @floop(:a)
:(1 + 5)
julia> @macroexpand @floop(:b)
:(1 + 10)
julia> @floop(:c)
ERROR: LoadError: KeyError: key :c not found
Stacktrace:
[1] getindex(::Dict{Symbol,Int64}, ::Symbol) at ./dict.jl:477
[2] @floop(::LineNumberNode, ::Module, ::QuoteNode) at ./REPL[27]:3
in expression starting at REPL[33]:1
julia> s = :a; @floop(s)
ERROR: LoadError: MethodError: no method matching @floop(::LineNumberNode, ::Module, ::Symbol)
Closest candidates are:
@floop(::LineNumberNode, ::Module, ::QuoteNode) at REPL[1]:2
in expression starting at REPL[2]:1
It's up to you how to actually store the actual values. A const
global dictionary would be an option, or just a simple if
chain.
(There can actually be other things than a Symbol
in a QuoteNode
, but it's difficult to splice them into the macro call, and they would also just raise a KeyError
, so I didn't include any extra handling of that.)