Hi Andrei,

On Monday, October 19, 2015 at 3:45:14 AM UTC-4, Andrei Zh wrote:
>
> I'm trying to write a macro for generation of new functions given their 
> name and arguments, e.g. given macro call: 
>
> @deffun foo(a, b)
>
> Coming from Common Lisp, I tried writing similar macros but failed to make 
them work. I've found that 

@some_macro function foo(a, b)

works a lot better. It's more verbose, but it composes better with other 
macros (kinda like Python decorators...) I use 

    @capture(fun_def, begin function fname_(args__) body__ end end)
    esc(:(function $(fname)($(args...))
         ...)


where @capture comes from macrotools.jl

 

>
> Important part is that both - name of a function and name of arguments - 
> should be preserved. My best attempt to do it looks like this: 
>
>         function $(esc(func))($(esc(args...)))
>

esc isn't magical, just `@show esc(:a)` to see what it does. You can write 
the arguments as 

($(map(esc, args)...))

and that _should_work, but last time I tried it doesn't because of a bug 
where Julia doesn't check properly for escaping in argument lists.


> `$(esc(func))` works pretty well, but `args` isn't a Symbol and so macro 
> compilation fails. 
>
> 1. How do I escape list of arguments here? 
> 2. How do I make this macro to also support argument types? E.g. given 
> macro call: 
>
>
> @deffun foo(a::Int, b::Float64)
>
> the following functions should be generated: 
>
> function foo(a::Int, b::Float64)
>     # do some stuff                                                       
>                                                                             
>                
> end
>
>
>
>

Reply via email to