Nice! I would comment on the following:
-
Your macro lacks input validation, which means users with typos or
misunderstandings will probably get weird error messages
-
I’d create the returned expression using a quote block, rather than :(),
to make it clear to source readers that it’s more than one statement (this
is a convention I’ve observed, but by no means a rule)
Others will probably react to different aspects of the implementation.
// T
On Wednesday, October 14, 2015 at 3:55:52 AM UTC+2, Tim Menzies 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
>
>