简体   繁体   中英

clojure quoting inside let

Quoting (both syntax and non-syntax) seems to fail to detect vars inside a let statement:

(let [foo 1] (eval `(print foo)))

This will generate an error or use whatever prior value bound was bound to foo in (def foo bar). Is there a way to make the syntax quote use the "local" variables that let defined instead?

Unquote to substitute the value:

(let [foo 1] (eval `(print ~foo)))

...or explicitly bind your variables:

(declare :^dynamic foo)
(binding [foo 1] (eval '(print foo)))

See Variable scope + eval in Clojure for details.

Kevin

You were close, this should do the work (basically, you should unquote the local var in the quoted statement)

(let [foo 1] (eval `(print ~foo)))

Also, while eval is certainly a valid language function, what is the overall goal? There may be better ways altogether.

Frank

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM