all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Robert Thorpe <rt@robertthorpeconsulting.com>
To: help-gnu-emacs@gnu.org
Subject: Lexical and Dynamic Scope
Date: Sat, 19 Jul 2014 21:06:48 +0100	[thread overview]
Message-ID: <8738dxglk7.fsf@robertthorpeconsulting.com> (raw)
In-Reply-To: <jwvegxhdy1t.fsf-monnier+gmane.emacs.help@gnu.org> (message from Stefan Monnier on Sat, 19 Jul 2014 14:12:25 -0400)

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> Because they can be encoded different things it makes sense calling
>> this lexical (because the value is simply read, not looked up) and not
>> "static".
>
> It's also called "static scoping".  And the name doesn't have much to do
> with the implementation technique used.  It's called "lexical" because
> the way a particular identifier use is matched to a particular variable
> only depends on the shape of the program text rather than depending on
> its run-time behavior.

I'll elaborate a bit....  What we're talking about here is where
variables are visible.  How those variables behave is a different
question.  They could only hold one type (classical static typing), hold
a set of types or hold any type ("dynamic" typing) depending on the
language.

Most languages have lexical scope.  The scope of a variable is defined
by a part of the program text.  For example, a variable Bar is defined
in a function Foo.  That means Bar is visible within Foo only.  The
section of text defines where variables can be accessed, that's why it's
called "lexical" or "static".  There can be multiple levels of lexical
scoping, for example in C some variables are visible everywhere in a
file.  One way of thinking about it is by thinking of a stack.  The
current lexical area and everything defined in it is the top of the
stack.  When the code exits that area that place on the stack disappears
(it's popped).  When a function call occurs a new entry on top of the
stack is created (a push).

Dynamic scope means that visibility follows code execution.  If a
variable Baz is defined by code executed in the past then it's visible
by present code.  If a local variable called Baz is defined then it's
used instead of the more global one.  The "higher level" value continues
to exist.  This can be thought of using stacks too, in a slightly
different way.  In the dynamic case there's a stack for every
*variable name*.  If a local variable is defined for a name that's
already used then a new value is pushed on the stack of variable values
and removed when the local area ends.

Stefan and the Emacs maintainers added lexical scope because it makes
Emacs Lisp faster and it's simpler to understand.

BR,
Rob



  reply	other threads:[~2014-07-19 20:06 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-15 13:47 run-with-timer does not display message Matthias Pfeifer
2014-07-15 13:53 ` Thorsten Jolitz
2014-07-18 16:26   ` Stefan Monnier
     [not found]   ` <mailman.5631.1405701027.1147.help-gnu-emacs@gnu.org>
2014-07-18 21:34     ` Emanuel Berg
2014-07-19 15:09       ` Sebastian Wiesner
2014-07-19 15:27         ` Eli Zaretskii
2014-07-19 17:08           ` Stefan Monnier
2014-07-19 18:04             ` Drew Adams
     [not found]           ` <mailman.5702.1405789759.1147.help-gnu-emacs@gnu.org>
2014-07-19 17:43             ` Emanuel Berg
2014-07-19 18:12               ` Stefan Monnier
2014-07-19 20:06                 ` Robert Thorpe [this message]
2014-07-20 12:15               ` Sebastian Wiesner
2014-07-21 13:26                 ` Stefan Monnier
     [not found]               ` <mailman.5708.1405793578.1147.help-gnu-emacs@gnu.org>
2014-07-20 19:27                 ` Emanuel Berg
2014-07-21 13:29                   ` Stefan Monnier
     [not found]                   ` <mailman.5813.1405950328.1147.help-gnu-emacs@gnu.org>
2014-07-21 22:17                     ` Emanuel Berg
2014-07-24  8:25                       ` Stefan Monnier
     [not found]               ` <mailman.5754.1405858540.1147.help-gnu-emacs@gnu.org>
2014-07-20 19:37                 ` Emanuel Berg
2014-07-20 20:11                   ` Drew Adams
2014-07-20 21:28                 ` dynamic and lexical scope, attempted summary with example (was: Re: run-with-timer does not display message) Emanuel Berg
2014-07-21  4:44                   ` dynamic and lexical scope, attempted summary with example Thien-Thi Nguyen
2014-07-21  6:36                     ` Thorsten Jolitz
     [not found]                     ` <mailman.5796.1405924598.1147.help-gnu-emacs@gnu.org>
2014-07-21 14:08                       ` Emanuel Berg
2014-07-21  9:23                   ` Michael Heerdegen
     [not found]                   ` <mailman.5792.1405917701.1147.help-gnu-emacs@gnu.org>
2014-07-21 13:36                     ` Emanuel Berg
2014-07-21 17:07                       ` Thien-Thi Nguyen
     [not found]                       ` <mailman.5826.1405962249.1147.help-gnu-emacs@gnu.org>
2014-07-21 22:11                         ` Emanuel Berg
     [not found]                   ` <mailman.5805.1405934637.1147.help-gnu-emacs@gnu.org>
2014-07-21 14:02                     ` Emanuel Berg
     [not found]       ` <mailman.5686.1405782584.1147.help-gnu-emacs@gnu.org>
2014-07-19 17:49         ` run-with-timer does not display message Emanuel Berg
     [not found] ` <mailman.5455.1405432451.1147.help-gnu-emacs@gnu.org>
2014-07-15 15:35   ` Emanuel Berg
     [not found] <mailman.5712.1405800439.1147.help-gnu-emacs@gnu.org>
2014-07-20 19:47 ` Lexical and Dynamic Scope Emanuel Berg
2014-07-24  8:50   ` Aurélien Aptel
     [not found]   ` <mailman.5948.1406191841.1147.help-gnu-emacs@gnu.org>
2014-07-24 22:16     ` Emanuel Berg
2014-07-24 22:28       ` Stefan Monnier
     [not found]       ` <mailman.5974.1406240948.1147.help-gnu-emacs@gnu.org>
2014-07-24 22:44         ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8738dxglk7.fsf@robertthorpeconsulting.com \
    --to=rt@robertthorpeconsulting.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.