Search code examples
emacslispelisplexical-closures

Creating a closure around a private function in elisp


In elisp I have the following

(setq lexical-binding t)



 (fset 'boom (cl-flet*
               ((tickle ()
                        (message "hi"))
                (pickle ()
                        (tickle)))
             (lambda ()
               (pickle))))
(boom)

When I run the boom function I get the error

Symbol's value as variable is void: --cl-pickle--

What's going on here?

My intent is I would like to wrap private/helper functions and variables in a closure so I can better organize my code than everything being global.


Solution

  • Don't (setq lexical-binding t). You don't want to execute a command that sets this variable. Instead you want to describe to Emacs that the code you write uses lexical binding, and for that you need to add:

    -*- lexical-binding:t -*-
    

    somewhere on the first line of the file.