From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Emanuel Berg Newsgroups: gmane.emacs.help Subject: dynamic and lexical scope, attempted summary with example (was: Re: run-with-timer does not display message) Date: Sun, 20 Jul 2014 23:28:58 +0200 Organization: Aioe.org NNTP Server Message-ID: <87bnsjsorp.fsf_-_@debian.uxu> References: <878unputvv.fsf@debian.uxu> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1405891824 20537 80.91.229.3 (20 Jul 2014 21:30:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 20 Jul 2014 21:30:24 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Jul 20 23:30:17 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 1X8ygT-0000zN-J1 for geh-help-gnu-emacs@m.gmane.org; Sun, 20 Jul 2014 23:30:17 +0200 Original-Received: from localhost ([::1]:59160 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X8ygS-0006vi-VL for geh-help-gnu-emacs@m.gmane.org; Sun, 20 Jul 2014 17:30:16 -0400 Original-Path: usenet.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.81.MISMATCH!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!news.stack.nl!aioe.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 54 Original-NNTP-Posting-Host: SIvZRMPqRkkTHAHL6NkRuw.user.speranza.aioe.org Original-X-Complaints-To: abuse@aioe.org User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Notice: Filtered by postfilter v. 0.8.2 Cancel-Lock: sha1:GUZ+3a0LlrBQhAMvY9vuVQsUsDA= Mail-Copies-To: never Original-Xref: usenet.stanford.edu gnu.emacs.help:206568 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:98839 Archived-At: As always, I think it is easier to discuss in terms of an example... This function is what started it all: (defun shortcut-to-file (key-prefix key file-prefix file) "Make shortcut with key KEY-PREFIX KEY to FILE-PREFIX FILE." (global-set-key (format "%s%s" key-prefix key) `(lambda () (interactive) (do-show-file (format "%s%s" ,file-prefix ,file)) ))) Is this correct? In dynamic scope: 1. At the 4th line, the line that starts with `format', we see use of the arguments to the parameters `key-prefix' and `key' - because they are inside the defun, it is clear that those refer the the arguments passed as parameters. 2. At the 7th, last line, `file-prefix' and `file' appears. Those are preceded by commas so, in combination with the backtick, their values will be inserted - hard-coded. This bypasses the whole problem because it eliminates all evaluation of symbols (it is like Caesar and the Gordian knot). The drawback is that the lambda can't be byte-compiled as it is formally "just" a list. 3. If the lambda wasn't backticked and shot with commas, the defun would evaluate fine and even calling it wouldn't raise an error. However, when the keystroke was actually hit, the lambda wouldn't be within the defun anymore - it would be in the wild, and `file-prefix' and `file' would be interpreted as symbols, i.e., global variables. Those are probably not defined... but if they were, with `setq' for example, those values would be used (this would make for a total mess). In lexical scope, the backtick and the commas aren't necessary. The lambda can be expressed as a lambda and nothing else, which means it can be byte-compiled. When the symbols are encountered, those aren't looked for as global variables, but rather the history of the place they occurred in code is drilled down, until a scope is encountered where they are defined, and those values are used. -- underground experts united