macro definition

69 views
Skip to first unread message

Mark Vogelsberger

unread,
Oct 9, 2012, 1:05:05 AM10/9/12
to juli...@googlegroups.com
Hi,

how do I define a macro that takes a function name and a variable number of parameters for that function?

Thanks,
Mark

Harlan Harris

unread,
Oct 9, 2012, 7:32:25 AM10/9/12
to juli...@googlegroups.com
Can you explain a bit what you're trying to do? You might not need to use macros.

Macros aren't functions. Their parameters are entirely arbitrary, so you can't enforce that the first argument is a function or whatever. You'd have to manually take apart the expression and check the types. 



--
 
 
 

Jeff Bezanson

unread,
Oct 9, 2012, 8:48:24 AM10/9/12
to juli...@googlegroups.com
"macro foo(args...)" will give you a macro with a variable number of
arguments, if that helps.

On Tue, Oct 9, 2012 at 1:05 AM, Mark Vogelsberger
<mark.vog...@gmail.com> wrote:
> --
>
>
>

Jasper

unread,
Oct 9, 2012, 4:18:47 PM10/9/12
to juli...@googlegroups.com
The macro gets expressions as arguments, these can be
numbers/integers/strings/Symbol's,Expr's etcetera.

You can inspect then by quoting stuff in the repl. If it is an `Expr`
object, then you have `:(a+b).head` -> `call`, `:(a+b).args`. I am not
entirely sure how robust that is. For instance `:(a+b+c)` and
`:(a|b|c)` does something different.(that in particular may warrant a
github issue..) I asked in another thread if maybe they should document
and keep simple the relation between expressions and their
representation.

Anyway, basically your macro can look at those objects, and construct
something. Tbh, rather often i make `Expr`s 'manually' with their
constructor, but if it is clear, you can use quoting. Also, you may
want to escape some stuff from the hygienic macro system(which
automatically replaces symbols with `gensym`s), `esc` does that.

Note that with macros you have to be careful you not to make a mess.
Don't make sublanguages or anything. Be careful about abstraction
leaks. Also be careful not to go too silly using syntax.
Reply all
Reply to author
Forward
0 new messages