help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Returning variable "references" under lexical binding


From: Barry OReilly
Subject: Re: Returning variable "references" under lexical binding
Date: Tue, 21 May 2013 10:41:51 -0400

>> Why not
>>
>>   (defun start-my-timer ()
>>     (let ((timer (make-timer ...))
>>       ...
>>       timer))
>>   (defun cancel-my-timer (timer)
>>     (cancel-timer timer))
>
> Because start-my-timer sets up callbacks that may repeatedly change the
> value of the "timer" variable:

But you said:

> I want my routine to return an object that can be used to cancel the
> most recently set timer.

How is the above start-my-timer inadequate then?

> (defun start-my-timer ()
>   (let (timer)
>     ;; ... (setq timer (make-timer ...)) ...
>     (lambda () timer)))
>
> (defun cancel-my-timer (timer)
>   (cancel-timer (funcall timer)))

Each invocation of this start-my-timer will return a different closure.
Whether start-my-timer returns a timer or a closure, you still have to keep
track of which one to pass to cancel-my-timer. So the use of closures
doesn't facilitate anything that I can see.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]