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: Robert Thorpe
Subject: Re: Real-life examples of lexical binding in Emacs Lisp
Date: Wed, 17 Jun 2015 23:07:33 +0100

Emanuel Berg <embe8573@student.uu.se> writes:
> I take it "lexical" refers to you can make it out by
> looking at the code.

Yes.

> "Dynamic" refers to it depends on the code and the
> program state in execution.

Yes.

> I agree those terms are confusing. To me, it sounds
> like they refer to call-by-value vs.
> call-by-reference, which isn't so.
>
> I'd call it "normal scope" vs.
> "stacked scope", perhaps.

Both cases are usually implemented using stacks.  Thinking about this
helps understanding.

In the lexical scope case we can think of one huge stack.  Each entry in
the stack can contain many elements.  When execution enters a "let" form
a new entry is created on the stack and the variables declared there are
stored in that entry.  They "shadow" other variables with the same name
- they take precedence over them.  When execution exits the let form
that stack entry is deleted.  This is usually how lexical scope is
implement too.

For dynamic scope we can think of each variable name as being associated
with it's own stack.  When execution reaches a let form a new value is
pushed onto the stack for the relevant name.  When the let form exits
it's deleted from the top of the stack.  Code always uses the
top-of-stack value, so earlier let forms are shadowed by later ones.
This is one way of implementing dynamic scope too, it's "shallow
binding".  I think it's how Elisp implements it.

Dynamic scope was an accident of how Lisp was first written.  See:
http://www-formal.stanford.edu/jmc/history/lisp/node4.html

The question of "extent" rather than "scope" brings up more problems.
So does "deep" vs "shallow" binding with dynamic scope.

BR,
Robert Thorpe



reply via email to

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