From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?UTF-8?B?QW5kcmVhcyBSw7ZobGVy?= Newsgroups: gmane.emacs.help Subject: Re: Real-life examples of lexical binding in Emacs Lisp Date: Fri, 29 May 2015 19:16:58 +0200 Message-ID: <55689F0A.5050307@easy-emacs.de> References: <87twuv7ele.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1432919858 27038 80.91.229.3 (29 May 2015 17:17:38 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 29 May 2015 17:17: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 Fri May 29 19:17:29 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 1YyNuS-0001ae-AT for geh-help-gnu-emacs@m.gmane.org; Fri, 29 May 2015 19:17:28 +0200 Original-Received: from localhost ([::1]:37148 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YyNuR-0001FF-HG for geh-help-gnu-emacs@m.gmane.org; Fri, 29 May 2015 13:17:27 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60743) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YyNuH-0001Bz-8D for help-gnu-emacs@gnu.org; Fri, 29 May 2015 13:17:18 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YyNuD-0007RB-NS for help-gnu-emacs@gnu.org; Fri, 29 May 2015 13:17:17 -0400 Original-Received: from mout.kundenserver.de ([212.227.17.24]:51548) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YyNuD-0007R7-C2 for help-gnu-emacs@gnu.org; Fri, 29 May 2015 13:17:13 -0400 Original-Received: from [192.168.178.31] ([95.119.228.213]) by mrelayeu.kundenserver.de (mreue101) with ESMTPSA (Nemesis) id 0MNtFb-1Z1Ndj10jz-007Wzm for ; Fri, 29 May 2015 19:17:12 +0200 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 In-Reply-To: <87twuv7ele.fsf@kuiper.lan.informatimago.com> X-Provags-ID: V03:K0:KSq4HwQvjBbl22A+xh7yAYrbneh/ejXxfmvTl79AEsx0+X0ENdV qx6ZbbcWl1fLVkof9gia6wPfIqg0owzLIJJM/5rgSFYH9Prxh1RpZZB5/ggKJC3VeZBD+3O PnTlKSQ4AU2zK4dMnjqU7zf1G9qAOmYcBJSlWsuiz3fCoq/3YBbv7jMAYHFYnI2sN1/PJ2v ZJW2JEtHkq0GhlCJb8sqA== X-UI-Out-Filterresults: notjunk:1; X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] X-Received-From: 212.227.17.24 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:104669 Archived-At: Am 29.05.2015 um 14:28 schrieb Pascal J. Bourguignon: > Marcin Borkowski writes: > >> Hi all, >> >> I googled a bit, and could not find /real-world/ examples of using >> lexical binding and its advantages /in Emacs Lisp/. I understand that >> it's a nice thing to be able to create closures, and that lexical >> binding is in general faster than dynamic binding (which is a bonus in >> itself), but could anyone show me a real /text editing/ problem that >> lexical binding solves, like something that is easier done with >> l.b. than with d.b.? (Examples of general-purpose programming problems >> made easier with l.b. are more or less obvious/easy to find, but Emacs >> is a text editor, after all, and this is its primary area.) > Lexical binding matters for two things: > > - it allows the creation of closures. > - it prevents the clobbering of variables. > > > Closures: > > A typical example, is visible in the thread "~`symbol-function' to > get code as list even when byte-compiled?": > > ;;;; -*- mode:emacs-lisp;lexical-binding:t;coding:utf-8 -*- > (defun add-one-shot-meat (hook fun) > (let ((name (gensym))) > (setf (symbol-function name) > (lambda () > (remove-hook hook name) > (funcall fun))) > (add-hook hook name))) > > Without lexical binding, fun and hook would be dynamic, and > therefore their bindings would disappear when add-one-shot-meat > returns. Therefore they would be undefined variable when the > function is called, or worse, they may be bound at that time by some > other function to something different. > > Compare: > > (setf lexical-binding t) > > (defun e (f) > (let ((v 42)) > (funcall f))) > > (let ((v 33)) > (e (lambda () v))) > > --> 33 > > > (setf lexical-binding nil) > > (defun e (f) > (let ((v 42)) > (funcall f))) > > (let ((v 33)) > (e (lambda () v))) > > --> 42 > > Thanks a lot all! Will take some time to work through the examples given. Cheers, Andreas