help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Real-life examples of lexical binding in Emacs Lisp


From: Tassilo Horn
Subject: Re: Real-life examples of lexical binding in Emacs Lisp
Date: Sat, 30 May 2015 10:28:54 +0200
User-agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux)

Marcin Borkowski <mbork@mbork.pl> writes:

Hi Marcin,

> I googled a bit, and could not find /real-world/ examples of using
> lexical binding and its advantages /in Emacs Lisp/.  I understand that
> it's a nice thing to be able to create closures, and that lexical
> binding is in general faster than dynamic binding (which is a bonus in
> itself), but could anyone show me a real /text editing/ problem that
> lexical binding solves, like something that is easier done with
> l.b. than with d.b.?  (Examples of general-purpose programming
> problems made easier with l.b. are more or less obvious/easy to find,
> but Emacs is a text editor, after all, and this is its primary area.)

The emacs paper has a section explaining why emacs used dynamic binding
from the start: http://www.gnu.org/software/emacs/emacs-paper.html#SEC17

However, to me the two example use-cases only motivate why you want to
be able to use dynamic scoping for special variables (defvar), not
really for local variables (let) or function parameters, although the
first Edit Picture example explicitly speaks of binding command
arguments but I don't understand it.

To me, having lexical scoping in general plus special, dynamically bound
variables is the ideal model.  If there wouldn't be special variables
anymore, the examples from the paper would need to be implemented by
setq-ing the variables and later re-setting them to their old value
which would be error-prone and wouldn't work if we ever get concurrency.
You can make special variables thread-local like in Clojure thus hiding
this additional complexity from programmers.

But there are good use-cases for dynamic variables also in "normal"
programs.  One example is to be able to bind some *error-handler*
dynamically in order to customize how to cope with errors that happen
far below the call stack.

I'm not able to come up with benefits of lexical scoping which are
especially important for editing text.  The general benefits of being
safer, faster, and having closures are still valid of course.

Bye,
Tassilo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]