Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how is it possible to intern macros in clojure?

Tags:

clojure

I want to do something like this for debugging purposes:

(intern 'clojure.core 'doc clojure.repl/doc)

but it is not letting me because the compiler says - cant take value of a macro.

is there another way?

like image 536
zcaudate Avatar asked Dec 12 '22 08:12

zcaudate


1 Answers

A macro is a function stored in a Var with :macro true in its metadata map. So, you can

  1. obtain the macro function itself by derefing the Var:

    @#'doc
    
  2. use intern to install a function as a macro by attaching appropriate metadata to the name symbol (see (doc intern) which promises to transfer any metadata provided in this way to the Var):

    (intern 'clojure.core
            (with-meta 'doc {:macro true})
            @#'clojure.repl/doc)
    

Using reader metadata is possible too, just remember to put it "inside the quote":

;; attaches metadata to the symbol, which is what we want:
' ^:macro doc

;; attaches metadata to the list structure (quote doc):
^:macro 'doc

^:macro is shorthand for ^{:macro true}.

like image 91
Michał Marczyk Avatar answered Jan 22 '23 10:01

Michał Marczyk