So I was trying to do something like this:
(defn bad-function [body]
(let [result “hello”]
body))
And then I tried to do something like this:(bad-function (println result))
And it failed. I didn’t understand why it would failed since I do something very similar in our…
Thanks. I was suspecting something like that. Now I know.
Ah, and I found a work around without having to use macros. After reading how scope works in clojure and playing with the REPL, I figured out that you can do something similar by passing a function:
(defn will-work [function]
(let [arg “hello”]
(function arg)
))
(will-work (fn [x] (println x)))
That way one can inject the local var binding into an independent function, and the function will be applied at the right time.
-
cabbagebot likes this
-
devscratchpad reblogged this from hackedy and added:
Thanks. I was suspecting something like that. Now I know. Ah, and I found a work around without having to use macros....
-
twitterbutlonger likes this
-
hackedy reblogged this from devscratchpad and added:
the problem is that the body is evaluated before being passed to the function. you want macros as an example (defn oh-no...
-
devscratchpad posted this