From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?UTF-8?B?S2FpIEdyb8Ofam9oYW5u?= Newsgroups: gmane.emacs.help Subject: Re: DynamicBindingVsLexicalBinding Date: Sun, 13 Oct 2013 15:46:19 +0200 Message-ID: <525AA42B.6030006@gmx.net> References: <52598D4A.2010901@easy-emacs.de> <871u3qjq0j.fsf@yandex.ru> <525A51BD.5040903@easy-emacs.de> 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 1381671980 5708 80.91.229.3 (13 Oct 2013 13:46:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 13 Oct 2013 13:46:20 +0000 (UTC) Cc: "help-gnu-emacs@gnu.org List" To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Oct 13 15:46:24 2013 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 1VVLzz-00045z-Mn for geh-help-gnu-emacs@m.gmane.org; Sun, 13 Oct 2013 15:46:23 +0200 Original-Received: from localhost ([::1]:33202 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VVLzz-0000xb-9I for geh-help-gnu-emacs@m.gmane.org; Sun, 13 Oct 2013 09:46:23 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33327) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VVLzh-0000xC-Gw for help-gnu-emacs@gnu.org; Sun, 13 Oct 2013 09:46:12 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VVLza-0007if-6w for help-gnu-emacs@gnu.org; Sun, 13 Oct 2013 09:46:05 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:58405) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VVLzZ-0007iZ-W4 for help-gnu-emacs@gnu.org; Sun, 13 Oct 2013 09:45:58 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1VVLzZ-0003ql-4p for help-gnu-emacs@gnu.org; Sun, 13 Oct 2013 15:45:57 +0200 Original-Received: from dslb-188-100-165-077.pools.arcor-ip.net ([188.100.165.77]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 13 Oct 2013 15:45:57 +0200 Original-Received: from kai.grossjohann by dslb-188-100-165-077.pools.arcor-ip.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 13 Oct 2013 15:45:57 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 44 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: dslb-188-100-165-077.pools.arcor-ip.net User-Agent: Postbox 3.0.8 (Macintosh/20130427) In-Reply-To: <525A51BD.5040903@easy-emacs.de> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 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:93986 Archived-At: Andreas Röhler wrote: > > That's interesting, but can hand-over functions also with dynamic binding. > > Do you have a real use-case where lexical-binding is superior? > > Can't see goodies from lexical binding beside a simplification for the > compiler. I think the key benefit is that lexical binding allows you to support closures, and with closures you can do cool things. In Node.js, you need to use closures for everything, due to its programming model. For example, when you query a DB in Node.js, you call a function passing it the query and another function to process the results: queryDb(sql, callback); Here, it is incredibly useful for "callback" to have access to local variables at the point where queryDb is called. (I mean variables that are local to where queryDb is called.) Some GUI frameworks allow you to specify a function to be called when the user presses a button, and here, too, it's nice for the function to have access to local variables from where the button was created. In Emacs, you write functions to be called when the user presses a key, and here the solution is that all the variables that the function needs are buffer-local. Of course, with dynamic binding you can do other cool things (you can let-bind a variable, then call a function which calls a function which calls yet other functions, and the innermost function will reference the value you just bound -- so you can pass parameters around without actually having to mention them on every function call). So each of the styles has their own advantage. I find that the idea that variables declared specially (e.g. with defvar) can be dynamically bound whereas everything else is lexically bound by default -- this idea is quite useful. For if you want to dynamically bind something to influence another function, you need to know which variables that other function uses -- and defvar is a good way to document this. Kai