From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: Real-life examples of lexical binding in Emacs Lisp Date: Sun, 14 Jun 2015 23:49:20 +0200 Organization: Informatimago Message-ID: <871thevu3j.fsf@kuiper.lan.informatimago.com> References: <87bnh3eqiv.fsf@mbork.pl> <87d21i6x15.fsf@kuiper.lan.informatimago.com> <878ubmvub6.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1434318620 2711 80.91.229.3 (14 Jun 2015 21:50:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 14 Jun 2015 21:50:20 +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 Jun 14 23:50:20 2015 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 1Z4FnH-0007W8-Lh for geh-help-gnu-emacs@m.gmane.org; Sun, 14 Jun 2015 23:50:19 +0200 Original-Received: from localhost ([::1]:59633 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z4FnG-0005W6-LK for geh-help-gnu-emacs@m.gmane.org; Sun, 14 Jun 2015 17:50:18 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 48 Original-X-Trace: individual.net NRXjjHTXv5k3L6CLeDEbag4wleG5ocrRzZvEvDmEHfVTqGRz6k Cancel-Lock: sha1:MGZjNzkxNGVkNjE0MGI4NjUyMWExYWRlNWQ4NmZiZDFmZmI4ZjM2ZA== sha1:SBzI2lRdR7H1pXY9QxXALiCIO6U= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:212647 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:104931 Archived-At: "Pascal J. Bourguignon" writes: > Stefan Monnier writes: > >>>> However, I would argue that for global variables, lexical binding would >>>> be useful too, even by default. >> >> I'm not sure what that means. But you can set a global value to >> a variable without declaring it as dynamically scoped: >> >> (setq my-lex-var 3) > > Wrong. Not. > Having a file lex.el: > ------------------------------------------------------------------------ > ;; -*- mode:emacs-lisp; lexical-binding:nil -*- > > (setq lexical? 33) > (defun f () > lexical?) > (defun g () > (let ((lexical? 42)) > (f))) > > (print (g)) > ------------------------------------------------------------------------ > > Then M-x load-file RET lex.el RET writes 42 in *Message* which shows > that lexical? IS a dynamic variable! If it was lexical, we would get > 33, because the lexical scope of the closure f is the global scope, > where lexical? as been bound to 33, not the local dynamic scope of the > function g when the variable lexical? is bound to 42. Oops, sorry, I intended to run two tests. With lexical-binding:t indeed, setq defines the global variable and leaves it lexical. Reloading the file with lexical-binding:t even suppresses the dynamic aspect of a previously defined (with defvar) dynamic variable. -- __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