Macros only work on syntax but not on values, as you noted in your
post. Here again:
julia> macro test(args...)
@show args
:()
end
julia> @test k[5] u[i]
args = (:(k[5]),:(u[i]))
()
Macros are processed during parsing of the source, no values exists at
that point. Jacob had a good short talk on this:
https://youtu.be/RYZkHudRTvI?list=PLP8iPy9hna6Sdx4soiGrSefrmOPdUWixM
Anyway, you could use this instead:
julia> function makefn(fname,label)
quote
function $fname()
label
end
export $fname
end
end
makefn (generic function with 1 method)
julia> cmds = Dict{Symbol,Symbol}(
:L => :leech, :R => :raise
)
Dict{Symbol,Symbol} with 2 entries:
:R => :raise
:L => :leech
julia> for kv in cmds
@eval $(makefn(kv[2],kv[1]))
end
julia> raise
raise (generic function with 1 method)
On Thu, 2016-02-11 at 22:37, Julia Tylors <[email protected]> wrote:
> Hi;
>
>
> I am having a problem of calling a macro with predetermined values from a
> Dictionary.
> How do i solve this problem?
>
> Thanks
>
>
> module X
> const cmds = Dict{Symbol,Symbol}(
> :L => :leech, :R => :raise
> )
>
> macro fun_gen(fname,label)
> efname = esc(fname)
> elabel = esc(label)
> quote
> function $(efname)()
> $elabel
> end
> export $(efname)
> end
> end
>
> for kv in cmds
> @fun_gen kv[2] kv[1]
> end
> end
>
> but this doesn't work.
>
> because @fun_gen exactly takes them as kv[2] kv[1]
>
> How can i do this?
> and what is the exact problem here?
>
> Thanks