From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Phil Sainty Newsgroups: gmane.emacs.devel Subject: Re: PATCH: Explicitly show how let works on global-variables Date: Wed, 05 Oct 2022 00:36:34 +1300 Message-ID: <08f6be68d07b1c3ee3c65f8fb6842eb3@webmail.orcon.net.nz> References: <83czb8vxdo.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14424"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Orcon Webmail Cc: Eli Zaretskii , emacs-devel@gnu.org To: Pedro Andres Aranda Gutierrez Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Oct 04 13:38:05 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ofgFR-0003bD-B2 for ged-emacs-devel@m.gmane-mx.org; Tue, 04 Oct 2022 13:38:05 +0200 Original-Received: from localhost ([::1]:43342 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ofgFQ-0004lj-AM for ged-emacs-devel@m.gmane-mx.org; Tue, 04 Oct 2022 07:38:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49834) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ofgE8-0003lP-Vg for emacs-devel@gnu.org; Tue, 04 Oct 2022 07:36:44 -0400 Original-Received: from smtp-2.orcon.net.nz ([60.234.4.43]:57231) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ofgE7-0007jP-Ee; Tue, 04 Oct 2022 07:36:44 -0400 Original-Received: from [10.253.37.70] (port=8221 helo=webmail.orcon.net.nz) by smtp-2.orcon.net.nz with esmtpa (Exim 4.90_1) (envelope-from ) id 1ofgDy-0004WO-RO; Wed, 05 Oct 2022 00:36:35 +1300 Original-Received: from ip-116-251-140-135.kinect.net.nz ([116.251.140.135]) via [10.253.37.253] by webmail.orcon.net.nz with HTTP (HTTP/1.1 POST); Wed, 05 Oct 2022 00:36:34 +1300 In-Reply-To: X-Sender: psainty@orcon.net.nz X-GeoIP: -- Received-SPF: pass client-ip=60.234.4.43; envelope-from=psainty@orcon.net.nz; helo=smtp-2.orcon.net.nz X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:296874 Archived-At: On 2022-10-04 21:09, Pedro Andres Aranda Gutierrez wrote: > I understood as local variable a 'value that was stored in the > function's stack' to be used in the scope of the let. That implied > (once again in my understanding) that the global system-time-locale > would not be affected and hence format-time-string would not see the > change in the value within the let. Since the addition of lexical binding to Emacs Lisp in Emacs 24.1, both results are possible depending on whether you are dealing with a dynamic or a lexical variable. I.e. given: (defun myfunc () foo) (let ((foo 'bar)) (myfunc)) If foo is a dynamic variable then the let form will return 'bar. If foo is a lexical variable, then you'd get this error: "let: Symbol’s value as variable is void: foo". Eli quoted the manual: Local variables created by a ‘let’ expression retain their value _only_ within the ‘let’ expression itself (and within expressions called within the ‘let’ expression); the local variables have no effect outside the ‘let’ expression. That "(and within expressions called within the ‘let’ expression)" is pretty ambiguous wrt dynamic vs lexical binding, and a few lines later it comments very briefly on this: in Emacs Lisp, the default scoping is dynamic, not lexical. (The non-default lexical binding is not discussed in this manual.) Which keeps the rest of the text accurate, yet in an almost-entirely unexplained manner. I suggest that at this point it has become pretty necessary for lexical binding to be discussed in this manual... * The *scratch* buffer, in which users will perform many if not most of their experiments, now uses lexical binding by default. * If enabled, auto-insert-mode adds lexical-binding: t to new elisp files by default. * IIRC most elisp files in Emacs core are now using lexical binding. * The emacs-lisp-mode mode-name treats dynamic binding as a warning. So while it's as true as ever that dynamic binding is the default, the fact that so many things nowadays default to *enabling* lexical binding really blurs this line, to the point where I think it's unreasonable to avoid discussing lexical binding in the introduction to emacs lisp, as the user will almost unavoidably be exposed to it. I think examples would be hugely helpful in explaining the difference between the two types of binding. -Phil