yazz.com
yazz.com

Reputation: 58826

How can I use single or double quote in Clojure Macro?

I am making a macro which uses single quotes. However, whenever I parse the macro the single quote text like:

'java' 

:is expanded to:

(quote java')

Does anyone know a way around this so that it doesn't expand to the quoted form?

Update

I have a kind of working workaround for this. I use:

(map (fn[x]
   (if (.startsWith (str x) "(quote ") 
     (apply str "'" (rest x)) 
     x)

: and this at least converts the 'text' to the string "'text'" which works for me

Upvotes: 0

Views: 386

Answers (2)

Joost Diepenmaat
Joost Diepenmaat

Reputation: 17761

Clojure macros apply to the result of the clojure reader's parsing of the source text. Has two effects:

  1. You will only get valid clojure datastructures as arguments to a macro.
  2. You can only output one valid clojure datastructure from a macro.

One of the consequences of this is that certain syntactical constructs can not made to work using macros (since they cannot be read) and require a different reader (meaning you cannot mix them into normal clojure source files).

As an example:

(`foo`)

is invalid syntax.

Upvotes: 1

Daniel Compton
Daniel Compton

Reputation: 14569

It looks like you're trying to make a string with single quotes wrapping java. To do this you need to use a backslash to escape the single quotes.

"\'java\'"

You were quoting the symbol java' which wasn't going to give you what you wanted.

Upvotes: 1

Related Questions