3

I have a function that returns another function, cons-then-eval-fn:

(defn cons-then-eval-fn [x]
  (fn [& e] (cons x (eval e))))

From that, I define two instances of this concept, one using cons-then-eval-fn, the other doing the same thing but with inlined code:

(def zero-a (cons-then-eval-fn 0))
(def zero-b (fn [& e] (cons 0 (eval e))))

With some arguments, these two functions behave identically (as I would expect):

(zero-a) => (0)
(zero-b) => (0)
(zero-a identity []) => (0)
(zero-b identity []) => (0)

But with these arguments, I see differing behavior:

(zero-b zero-b identity []) => (0 0)
(zero-a zero-a identity []) =>
   IllegalArgumentException No matching ctor found

Can anyone please help me understand why this happens?

1 Answer 1

2

You must not eval function objects. eval is for symbols, lists, and so on: source code to be fed to the compiler. An already-compiled function object is an invalid argument to eval; it happens to work in some cases and not in others (specifically, for closures it fails and for functions with no captured scope it works, but this is not guaranteed).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.