From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: Real-life examples of lexical binding in Emacs Lisp Date: Sun, 14 Jun 2015 12:52:44 +0200 Message-ID: <87fv5u5zpm.fsf@mbork.pl> References: <87bnh3eqiv.fsf@mbork.pl> <874mmuxyd5.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1434279201 11939 80.91.229.3 (14 Jun 2015 10:53:21 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 14 Jun 2015 10:53:21 +0000 (UTC) To: Help Gnu Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Jun 14 12:53:12 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 1Z45XL-0004VJ-JZ for geh-help-gnu-emacs@m.gmane.org; Sun, 14 Jun 2015 12:53:11 +0200 Original-Received: from localhost ([::1]:58225 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z45XK-0002uv-Kv for geh-help-gnu-emacs@m.gmane.org; Sun, 14 Jun 2015 06:53:10 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:44832) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z45X9-0002uq-PF for help-gnu-emacs@gnu.org; Sun, 14 Jun 2015 06:53:00 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z45X6-00028C-H4 for help-gnu-emacs@gnu.org; Sun, 14 Jun 2015 06:52:59 -0400 Original-Received: from mail.mojserwer.eu ([2a01:5e00:2:52::8]:57401) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z45X6-000265-9s for help-gnu-emacs@gnu.org; Sun, 14 Jun 2015 06:52:56 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id F11EB4F6080 for ; Sun, 14 Jun 2015 12:52:51 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id uuNWBcxlZ7EH for ; Sun, 14 Jun 2015 12:52:49 +0200 (CEST) Original-Received: from localhost (unknown [109.232.24.146]) by mail.mojserwer.eu (Postfix) with ESMTPSA id 59A1D4F607B for ; Sun, 14 Jun 2015 12:52:49 +0200 (CEST) In-reply-to: <874mmuxyd5.fsf@gnu.org> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a01:5e00:2:52::8 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:104918 Archived-At: OK, so I finally got to study this discussion. (It seems I have kind of a talent for inadvertently starting long discussions...) On 2015-05-30, at 10:28, Tassilo Horn wrote: > Marcin Borkowski writes: > > Hi Marcin, > >> 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.) > > The emacs paper has a section explaining why emacs used dynamic binding > from the start: http://www.gnu.org/software/emacs/emacs-paper.html#SEC17 Yes, I'm aware of that, and I read this paper some time ago (even before asking my question). > However, to me the two example use-cases only motivate why you want to > be able to use dynamic scoping for special variables (defvar), not > really for local variables (let) or function parameters, although the > first Edit Picture example explicitly speaks of binding command > arguments but I don't understand it. > > To me, having lexical scoping in general plus special, dynamically bound > variables is the ideal model. If there wouldn't be special variables > anymore, the examples from the paper would need to be implemented by > setq-ing the variables and later re-setting them to their old value > which would be error-prone and wouldn't work if we ever get concurrency. > You can make special variables thread-local like in Clojure thus hiding > this additional complexity from programmers. > > But there are good use-cases for dynamic variables also in "normal" > programs. One example is to be able to bind some *error-handler* > dynamically in order to customize how to cope with errors that happen > far below the call stack. > > I'm not able to come up with benefits of lexical scoping which are > especially important for editing text. The general benefits of being > safer, faster, and having closures are still valid of course. Exactly. What I'm curious is how lexical scoping might make some tasks *connected to editing* easier/more natural. > Bye, > Tassilo Thanks, -- Marcin Borkowski http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski Faculty of Mathematics and Computer Science Adam Mickiewicz University