Emacs Variables

Emacs variables are at once both easier and more difficult than in other languages. On the one hand, Emacs variables are typeless so you can put whatever you like in them and even use the same variable to hold different types at different times. Whether this is a good or bad idea is a religious issue and one I don’t want to litigate here.

On the other hand, Emacs variables can have differing scoping rules that can seem arcane and confusing, especially if you’re used to “normal” languages. The situation is more complicated than in other Lisps because Elisp also has buffer-local-variables and custom-variables with their own scopes and setting rules.

Over at (with-emacs, Clemens Radermacher has a post that gives a nice introduction to Elisp variables. It’s the first in a series that Radermacher is planning on Elisp fundamentals that he is aiming at beginners. One of the hardest things for Lisp n00bs to grasp is the difference between lexical and dynamic scoping. Dynamic variables are (much) more than just global variables and can have some surprising behaviors. As Radermacher says, the situation is exacerbated with Elisp because, for historical reasons, dynamic variables are the default and it’s only recently that lexical variables have been added. Even with their official introduction, lexical variables require some special handling. Radermacher does a pretty job of explaining all this so if you’re new to Elisp, the post is worth reading to help get you started.

Update [2020-02-16 Sun 13:25]: Fixed link to Radermacher’s post.

This entry was posted in General and tagged . Bookmark the permalink.