From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Johan Andersson Newsgroups: gmane.emacs.help Subject: Re: Timer variable binding Date: Wed, 8 Jan 2014 07:25:51 +0100 Message-ID: References: <87d2k3ydij.fsf@yahoo.fr> <1389131851.70949.YahooMailNeo@web172805.mail.ir2.yahoo.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: ger.gmane.org 1389162358 13391 80.91.229.3 (8 Jan 2014 06:25:58 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 8 Jan 2014 06:25:58 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Nicolas Richard Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jan 08 07:26:05 2014 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 1W0mab-0007mU-K8 for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Jan 2014 07:26:05 +0100 Original-Received: from localhost ([::1]:45241 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W0mab-0005wg-0O for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Jan 2014 01:26:05 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:42727) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W0maP-0005wb-1L for help-gnu-emacs@gnu.org; Wed, 08 Jan 2014 01:25:53 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W0maO-0005IZ-3U for help-gnu-emacs@gnu.org; Wed, 08 Jan 2014 01:25:52 -0500 Original-Received: from mail-oa0-x230.google.com ([2607:f8b0:4003:c02::230]:46669) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W0maN-0005IV-Tw for help-gnu-emacs@gnu.org; Wed, 08 Jan 2014 01:25:52 -0500 Original-Received: by mail-oa0-f48.google.com with SMTP id k1so1326634oag.35 for ; Tue, 07 Jan 2014 22:25:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=XpAkAiROrbP3uYYnn44Hk3jgEvEBpQoUY985RRCZbDw=; b=MUCY49N0telTuJElbRH0BRspctJ0ayKk2y3oFNYJ6KG6QQMvy/4AtU1evj8cttnEDI uNfRsPelNW1tp5kGGwyELkTJZlObUkvAhEYi5zjM4l2p3tfxgrDuXe8YSIjGZZXVqzpa hgGijG66d95BKp7MK3YnIMWY/sw9R68a0qXcg87P+bj975Uz7TUBfer3aQHaSOrl6f9H r0AquZx5IkNihvA0nvN3GWd4NI7aTomAPAeeEQOgPyQgqjffy9CFNbh60QX8srXp72eA 4Sv4t+g32QLNKLfgXi4+inPcamYFA2bVQKLk15iRT79EnWEimpYqNc8vx+j3I/gISXqS LDRA== X-Received: by 10.182.99.231 with SMTP id et7mr79541799obb.10.1389162351251; Tue, 07 Jan 2014 22:25:51 -0800 (PST) Original-Received: by 10.182.154.73 with HTTP; Tue, 7 Jan 2014 22:25:51 -0800 (PST) Original-Received: by 10.182.154.73 with HTTP; Tue, 7 Jan 2014 22:25:51 -0800 (PST) In-Reply-To: <1389131851.70949.YahooMailNeo@web172805.mail.ir2.yahoo.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c02::230 X-Content-Filtered-By: Mailman/MimeDel 2.1.14 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:95305 Archived-At: Of course. Obvious now that you say it. Lexical binding it is. Thanks very much for the answers! On Jan 7, 2014 10:57 PM, "Nicolas Richard" wrote: > > let (my-var=10) -> run-at-time -> *magic* -> callback-function > > Looks to me like this should work? But why doesn't it? ;) > > By the time the callback is called, the let form is long gone. The > callback-function is stored in some place (namely, in a timer structure in > the global variable timer-list) and then, after some time, another piece of > emacs code calls that function. At that moment, there's no more let binding. > > Why it works with lexical binding is because the lambda is made into a > closure, which then knows what the symbol my-var is. > > In a buffer where lexical-binding is t, you can try evalling > (setq foobar (let ((my-var 0)) (lambda () (incf my-var) (message "my-var: > %s" my-var)))) > the answer will be: > (closure ((my-var . 0) t) nil (setq my-var (1+ my-var)) (message "my-var: > %s" my-var)) > if you now call (funcall foobar), you'll get "my-var: 1" > and now, evalling foobar gives: > (closure ((my-var . 1) t) nil (setq my-var (1+ my-var)) (message "my-var: > %s" my-var)) > This is magic ! > > Doing the same without lex-bind will give you a lambda, then an error, > then the same lambda. > > -- > > Nico. > >