From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Robert Thorpe Newsgroups: gmane.emacs.help Subject: Lexical and Dynamic Scope Date: Sat, 19 Jul 2014 21:06:48 +0100 Message-ID: <8738dxglk7.fsf@robertthorpeconsulting.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1405800458 1130 80.91.229.3 (19 Jul 2014 20:07:38 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 19 Jul 2014 20:07:38 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Jul 19 22:07:31 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1X8aum-0001BR-I1 for geh-help-gnu-emacs@m.gmane.org; Sat, 19 Jul 2014 22:07:28 +0200 Original-Received: from localhost ([::1]:55411 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X8aul-00039S-Rd for geh-help-gnu-emacs@m.gmane.org; Sat, 19 Jul 2014 16:07:27 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:38077) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X8auU-00038I-45 for help-gnu-emacs@gnu.org; Sat, 19 Jul 2014 16:07:16 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X8auN-0000q6-F0 for help-gnu-emacs@gnu.org; Sat, 19 Jul 2014 16:07:10 -0400 Original-Received: from outbound-smtp04.blacknight.com ([81.17.249.35]:51779) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X8auN-0000ag-8j for help-gnu-emacs@gnu.org; Sat, 19 Jul 2014 16:07:03 -0400 Original-Received: from mail.blacknight.com (pemlinmail01.blacknight.ie [81.17.254.10]) by outbound-smtp04.blacknight.com (Postfix) with ESMTP id 027A598839 for ; Sat, 19 Jul 2014 20:06:34 +0000 (UTC) Original-Received: (qmail 32366 invoked from network); 19 Jul 2014 20:06:50 -0000 Original-Received: from unknown (HELO RTLaptop) (rt@robertthorpeconsulting.com@[109.79.221.15]) by 81.17.254.9 with ESMTPSA (DHE-RSA-AES128-SHA encrypted, authenticated); 19 Jul 2014 20:06:49 -0000 In-Reply-To: (message from Stefan Monnier on Sat, 19 Jul 2014 14:12:25 -0400) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 81.17.249.35 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:98803 Archived-At: Stefan Monnier 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