Allow character in macro

I'm stuck on the seemingly basic thing. I have a namespace where I have some definitions:

(ns my-namespace) (def my-definition "HELLO") (def my-definition2 "HI") 

Now I want to use the vars value in my-namespace in the macro, but I want to get the characters dynamically. For instance.

 (defmacro my-macro [n] (-> "my-namespace/my-definition" symbol resolve var-get)) 

Getting a character this way works in a function (as long as the namespace is loaded), but not in the macro.

In a macro, a character cannot be resolved. I tried to quote and make inconspicuous, but still does not work.

Is it possible to use the value of the created character in a macro? If so, how?

+5
source share
2 answers

The character cannot be resolved because the namespace in which it is defined is not loaded. You can load the namespace

 (require 'my-namespace) 

or in a namespace declaration:

 (ns macro-expansion-ns (:require [my-namespace])) 
+2
source

Try the following:

 (defmacro my-macro [str] (-> str symbol resolve deref)) 
+3
source

Source: https://habr.com/ru/post/1259975/


All Articles