SaltyEgg
SaltyEgg

Reputation: 1558

Is it possible to expand a macro into several elements?

I want a macro my-macro which can expand to 1 2 3 rather than (1 2 3), so that

(list (my-macro) 4 5) -> (1 2 3 4 5)

Is this possible?

Upvotes: 1

Views: 199

Answers (2)

6502
6502

Reputation: 114579

No.

Neither a macro nor a read macro can do that in Common Lisp.

The only solution if you really need this is to write a full reader yourself where you don't use read at all (the problem is that read will recursively call itself, and not your version).

A full compliant reader is quite a complex thing to do, but it can be simple if you only need a subset of the features and you don't need to use it for example to read Common Lisp code written by others.

Upvotes: 4

Vatine
Vatine

Reputation: 21288

No, macros cannot expand to more than one value. The typical thing to do when you need a macro to expand to multiple pieces of code is to wrap the return value in a progn.

In your example in the comments, it looks as if you are using macros not as syntactic abstractions, but as cheap-and-cheerful function optimizations, the usual response to that is "please don't do it, it is wrong and doesn't actually do what you want".

Upvotes: 6

Related Questions