4

I'd rather not use cl-lib and cl at the same time. However, I really like using lexical-let to specify the usage of lexical binding on a more granular level.

Is there any equivalent in cl-lib or vanilla Emacs Lisp?

Drew
  • 75,699
  • 9
  • 109
  • 225
PythonNut
  • 10,243
  • 2
  • 29
  • 75
  • It took me several hours, but I now have my own CL library with different names. You may wish to create your own at some point when you have some free time. My main motivation was keeping all custom libraries fully functional while being able to try out the latest Emacs Trunk with less likelihood of revisions by the Emacs team breaking my daily workflow. – lawlist Aug 31 '15 at 03:47
  • One alternative is to create a global variable and set the value, which can be seen from within a nested `lambda`. Other alternatives, include feeding the nested `lambda` the variable from a previous setting, if it is known. I haven't tried a buffer-local variable, but that may work too -- i.e., (with-current-buffer xyz my-local-variable) -- i.e., get the value from within the nested `lambda` and use it. – lawlist Aug 31 '15 at 03:57
  • I don't know enough CL to be sure, but my impression from the `lexical-let` docstring is that it works the same way as CL's `let`, and therefore it may be a deficiency of `cl-lib` (or rather `cl-macs`) that it doesn't implement `cl-let` as an alternative to `lexical-let`? – phils Aug 31 '15 at 05:32

2 Answers2

5

cl and cl-lib are not "either or". The former requires the latter and defines lexical-let.

If you want lexical bindings, you can turn them on using the variable lexical-binding.

You can also set it on a per-file basis using file variables.

sds
  • 5,928
  • 20
  • 39
  • I want to specify lexical binding for a single specific binding, not all the bindings in the file. How can I do that? – PythonNut Aug 31 '15 at 03:16
  • I don't think you can do that without `cl`. Why would you want to?! – sds Aug 31 '15 at 03:19
  • mainly because it's simpler and features less side-effects. – PythonNut Aug 31 '15 at 03:21
  • @PythonNut I think the question is not why you want to use it there, but why do you not want to use it elsewhere in your file ? – YoungFrog Aug 31 '15 at 12:26
  • @YoungFrog I just don't like enabling things when they aren't needed. It's the same reason I don't use `setf` instead of `setq`, even though it would work. – PythonNut Sep 05 '15 at 16:10
  • @PythonNut I think the only reason why it isn't enabled by default is backward compatibility. New code should use lexical-binding whenever possible. – YoungFrog Sep 05 '15 at 16:50
  • @YoungFrog I suppose I always avoided it because it's supposed to be slower. I've enabled it for all my files and everything seems fine, so I'll just keep it that way, then. Thanks! – PythonNut Sep 05 '15 at 17:03
5

Simply require library cl at compile time, to get the use of its macros (and not get any runtime load). That is where macro lexical-let is defined.

So all you need is this, to use lexical-let:

(eval-when-compile (require 'cl)) ;; lexical-let

(I put the stuff I use from the library in a comment like that, just to let me know what I'm using from it.)

Oh, and lexical-let works in all Emacs versions (at least Emacs 20 and later). And yes, it lets you use dynamic binding by default and use lexical binding in a granular way, when you want it.

(cl.el is vanilla Emacs, BTW, not a 3rd-party library. It is simply not preloaded. It has the same status as something like Org mode or Calc or dired-x.el.)

Drew
  • 75,699
  • 9
  • 109
  • 225