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: Marcin Borkowski
Subject: Re: Real-life examples of lexical binding in Emacs Lisp
Date: Sun, 14 Jun 2015 12:52:44 +0200

OK, so I finally got to study this discussion.  (It seems I have kind of
a talent for inadvertently starting long discussions...)

On 2015-05-30, at 10:28, Tassilo Horn <tsdh@gnu.org> wrote:

> 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

Yes, I'm aware of that, and I read this paper some time ago (even before
asking my question).

> 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.

Exactly.  What I'm curious is how lexical scoping might make some tasks
*connected to editing* easier/more natural.

> Bye,
> Tassilo

Thanks,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



reply via email to

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