Search code examples
emacsclosureselisp

How do I do closures in Emacs Lisp?


I'm trying to create a function on the fly that would return one constant value.

In JavaScript and other modern imperative languages I would use closures:

function id(a) {
    return function() {return a;};
}

but Emacs lisp doesn't support those.

I can create mix of identity function and partial function application but it's not supported either.

So how do I do that?


Solution

  • Stupid idea: how about:

    (defun foo (x)
      `(lambda () ,x))
    
    (funcall (foo 10))  ;; => 10