Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access the source position while evaluating a clojure macro?

Inside of a macro I would like to get the source file and position of the caller of the macro.

Something like:

(defmacro my-macro []
  `(prn ~*source-position*))

where *source-position* contains information about the source position of the file which is currently compiled while evaluating the macro.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When calling this macro, it should print the source file, line and column of the caller.

>Solution :

Use the &form symbol to get the position of the original form during macroexpansion.

=>(defmacro m [x] `(prn '~(meta &form)))
#'user/m
=>(m (+ 1 3 3))
{:line 1, :column 1}
nil

And source-path should give you the file name.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading