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: Wed, 17 Jun 2015 12:49:47 +0200 Organization: Informatimago Message-ID: <871thatxro.fsf@kuiper.lan.informatimago.com> References: <87bnh3eqiv.fsf@mbork.pl> <874mmuxyd5.fsf@gnu.org> <87k2v6wmpy.fsf@kuiper.lan.informatimago.com> <87vbendwq5.fsf@debian.uxu> 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 1434538709 16867 80.91.229.3 (17 Jun 2015 10:58:29 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 17 Jun 2015 10:58:29 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jun 17 12:58:28 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 1Z5B2z-0000tR-Kf for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Jun 2015 12:58:21 +0200 Original-Received: from localhost ([::1]:45672 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z5B2t-00055j-Or for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Jun 2015 06:58:15 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 94 Original-X-Trace: individual.net 2JUjXHzJ0dgGnuodYr6Udw/y30EVyU9u9SY5GbVXoSlYYWv0Ft Cancel-Lock: sha1:MzA0ZjZkZjNiMjAzNmViMTllYjE1OTAxYmMwYmE1Yjk5MDk1NDJhNg== sha1:fb5B2jcH22RdsHpcIpIxfQ+4mWA= 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:212717 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:105001 Archived-At: Andreas Röhler writes: > Am 17.06.2015 um 02:06 schrieb Emanuel Berg: >> Jim Diamond >> writes: >> >>> Really? Are there well-agreed-upon studies showing >>> those things? Or are they your opinion? >>> >>> It strikes me that lexical scoping is easier to >>> implement for compiled languages (that is an "off >>> the cuff" comment from someone (me) with basic >>> knowledge of compiler construction). But if lexical >>> scoping is "more natural", is that because more >>> people were "brought up" with lexically-scoped >>> languages than dynamically-scoped languages? >> This discussion is much easier to have if that >> confusing terminology is dropped for a second and we >> instead study the simple example of a `let' form: >> >> (let ((scratch-buffer "*scratch*")) >> (when (bufferp scratch-buffer) >> (kill-buffer scratch-buffer) )) >> >> Here we have one piece of data which is used twice, so >> that data is named and when it is used it is >> indirectly refered to. >> >> In this example, what is natural to me? Answer: >> I don't expect `let' to affect any other code than the >> code in the `let' itself! And this is "lexical >> scoping". > > Nonetheless, that's the way Emacs acted all the time, while called > "dynamically" scoped. > > Now with "lexical" we have instead an injection, if a function with > same arguments' symbol is called inside let. > > Seems neither "lexical" nor "dynamic" express the real thing. To be more concrete, here is a case where something wrong happens: (setf lexical-binding nil) (defun do-something (arg) (format "\n%S\n" arg)) (defun some-function (arg) (setf scratch-buffer (get-buffer-create " *some-function scratch buffer*")) (with-current-buffer scratch-buffer (insert (do-something arg)))) (defun some-other-function () (with-current-buffer scratch-buffer (buffer-substring (point-min) (point-max)))) ;; and then in some unrelated code in a different file: (setq lexical-binding nil) (let ((scratch-buffer (get-buffer-create "*scratch*"))) (with-current-buffer scratch-buffer (insert "hello")) (some-function "Howdy?") (with-current-buffer scratch-buffer (buffer-substring (point-min) (point-max)))) --> " \"Howdy?\" \"Howdy?\" " ; instead of "hello" !!! On the other hand, if you use lexical binding: (setq lexical-binding t) (let ((scratch-buffer (get-buffer-create "*scratch*"))) (with-current-buffer scratch-buffer (insert "hello")) (some-function "Howdy?") (with-current-buffer scratch-buffer (buffer-substring-no-properties (point-min) (point-max)))) --> ";; This buffer is for notes you don't want to save, and for Lisp evaluation. ;; If you want to create a file, visit that file with C-x C-f, ;; then enter the text in that file's own buffer. hello" then this independent code stays independent and clean, and no other function may fuck it. -- __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