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: Pascal J. Bourguignon
Subject: Re: Real-life examples of lexical binding in Emacs Lisp
Date: Wed, 17 Jun 2015 02:43:45 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Jim Diamond <Jim.Diamond@deletethis.AcadiaU.ca> writes:

> On 2015-06-14 at 08:31 ADT, Pascal J. Bourguignon <pjb@informatimago.com> 
> wrote:
>> Marcin Borkowski <mbork@mbork.pl> writes:
>>
>>> Exactly.  What I'm curious is how lexical scoping might make some tasks
>>> *connected to editing* easier/more natural.
>
> <snip>
>
>> Writing programs is easier and more natural with lexical scoping, IN
>> GENERAL!
>
> <snip>
>
> Really?  Are there well-agreed-upon studies showing those things?
> Or are they your opinion?

Yes, there are well-agreed-upon studies showing this.  This is the
reason ALL programming languages created since 1970, use lexical binding
exclusively.


> It strikes me that lexical scoping is easier to implement for compiled
> languages (that is an "off the cuff" comment from someone (me) with
> basic knowledge of compiler construction).  But if lexical scoping is
> "more natural", is that because more people were "brought up" with
> lexically-scoped languages than dynamically-scoped languages?

No, this is for theorical reasons, and in practice, because lexical
scoping allows to understand the semantics of programs by just looking
at the source, without executing it, and therefore make it easier on the
programmers, and ease debugging and maintainance.


> A few versions of emacs ago something I was using went from dynamic
> scoping to lexical scoping.  Working around that change was not
> trivial, casting suspicion on the universality of "easier".

The not trivial comes from the fact that it was wanted to perform the
transition without rewriting all the existing elisp code, not only in
the GNU emacs distribution, but also all the unpublished elisp code on
the disks of all the users.  Therefore, in emacs:

1- dynamic binding is still the default,

2- lexical binding is optional (you have to mark a file specially to get
   it).

There may also be some technicalities with respect to the buffer-local
variables which is an emacs specific complexity; I've not looked into it
yet.



Also, I would say that in emacs, given the number of hooks there are,
lexical binding should demonstrate definite and overwhelming advantages
over dynamic binding, since there is no closure with dynamic binding.

For example, compare:

    (setq lexical-binding nil)
    (let ((message (read-from-minibuffer "Message: ")))
      (push (lambda () (insert message)) text-mode-hook))
    [enter some text, type RET,  then switch to *scratch* and:]
    M-x text-mode RET
    (pop text-mode-hook)

with:

    (setq lexical-binding t)
    (let ((message (read-from-minibuffer "Message: ")))
      (push (lambda () (insert message)) text-mode-hook))
    [enter some text, type RET,  then switch to *scratch* and:]
    M-x text-mode RET
    (pop text-mode-hook)

 
-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

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