I don't think there is much documentation for Julia-macros around but if
you know LISP then you're ahead in the game anyway.  So it's mostly
learning by looking at other folk's macros:

My package https://github.com/mauro3/Parameters.jl does something
similar to your example.  So you could have a look at that and get some
inspiration.

Also xdump and Meta.show_sexpr are your friends in dissecting Expr.

On Tue, 2015-10-13 at 23:18, Tim Menzies <[email protected]> wrote:
> this is day3 of julia so pardon dumb questions
>
> i had some trouble  finding tutorials on julia macros. i've read the
> textbooks but i suspect there is somewhere else to look (i say that since i
> would have thought that the following would exist in standard Julia, but i
> could not find it). so, two questions
>
> Q1) where to find julia macro tutorials (beyond the standard manual)?
>
> Q2) here's my first julia macro to do something like LISP's defstruct where
> i can define a type and its init contents all at one go. comments? traps
> for the unwary? improvements?
>
> # e.g. @def emp age=0 salary=10000
>
> # autocreates a constructor emp0 that returns and emp
>
> # initialized with 0,10000
>
> macro has(typename, pairs...)
>     name = esc(symbol(string(typename,0)))
>     tmp  = esc(symbol("tmp"))
>     ones = [x.args[1] for x in pairs]
>     twos = [x.args[2] for x in pairs]
>     :(type $(typename)
>          $(ones...)
>       end;
>       function $(name)()
>          $(typename)($(twos...))
>       end)
> end
>
>
> thanks!
> tiim menzies

Reply via email to