Re: [julia-users] Calling a macro to generate functions

2016-02-12 Thread Stefan Karpinski
At a higher level, you seem to be doing something that involves a lot of
metaprogramming. Can you say a little more about the higher level goal and
why it entails so much macrology?

On Thu, Feb 11, 2016 at 4:37 PM, Julia Tylors  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
>


Re: [julia-users] Calling a macro to generate functions

2016-02-11 Thread Mauro
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  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


[julia-users] Calling a macro to generate functions

2016-02-11 Thread Julia Tylors
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