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?

  • 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 3: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 3: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 5:32
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.

  • 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 3:16
  • I don't think you can do that without cl. Why would you want to?! – sds Aug 31 '15 at 3:19
  • mainly because it's simpler and features less side-effects. – PythonNut Aug 31 '15 at 3: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 5 '15 at 16:10
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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.