Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the conventional way to indicate an error inside defmacro?

What is the conventional, well-behaved way for a macro to indicate that it's been passed invalid arguments?

(defmacro defthisthing [name & definitions] . . .)

I'm writing a macro right now to accept a whole bunch of definitions. If the same thing gets defined twice, the macro should complain. Similarly, if one of the definitions uses a term that's not defined elsewhere in the same macro invocation, the macro should complain, hopefully with line and column numbers so the programmer can see exactly where the error is.

I'm currently thinking that throwing an exception makes the most sense, because invalid macro arguments are in fact a compilation error. Everything should shut down no differently than if the compiler found unbalanced parentheses.

If that's correct, what is the conventional exception to throw? And how do you include the filename and line number of the offending snippet of code?

And if that's not correct, what is the more Clojurely approach?

like image 983
Ben Kovitz Avatar asked Apr 12 '16 01:04

Ben Kovitz


1 Answers

Throwing exceptions sounds fine. I just checked the Clojure source and this is how it is done there:

(defmacro let ...) calls (defmacro assert-args ...), which throws exceptions if the arguments don’t meet their requirements.

like image 159
Asim Jalis Avatar answered Sep 22 '22 08:09

Asim Jalis