all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Returning variable "references" under lexical binding
@ 2013-05-20 20:35 Sean McAfee
  2013-05-21  1:18 ` Barry Margolin
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Sean McAfee @ 2013-05-20 20:35 UTC (permalink / raw)
  To: help-gnu-emacs

I recently tried writing my first non-toy code that employs lexical
binding.  It's a routine that sets up a series of idle timers, storing
each successive timer object into the same lexical variable.

I want my routine to return an object that can be used to cancel the
most recently set timer.  If I were writing this code in the days prior
to lexical binding, I might have dono something like this:

(defun start-my-timer ()
  (let ((timer (gensym)))
    ;; ... (set timer (make-timer ...)) ...
    timer))

(defun cancel-my-timer (timer)
  (cancel-timer (symbol-value timer)))

The documenentation for lexical variables cautions against treating them
as symbols, specifically stating that functions like symbol-value will
not work.  So I wrote my routine to return a closure:

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

(defun cancel-my-timer (timer)
  (cancel-timer (funcall timer)))

This works, but is it the "correct" way to do this?


^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: Returning variable "references" under lexical binding
@ 2013-05-21 14:41 Barry OReilly
  0 siblings, 0 replies; 11+ messages in thread
From: Barry OReilly @ 2013-05-21 14:41 UTC (permalink / raw)
  To: help-gnu-emacs

>> 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.


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-05-23  1:01 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-20 20:35 Returning variable "references" under lexical binding Sean McAfee
2013-05-21  1:18 ` Barry Margolin
2013-05-21  3:18 ` Stefan Monnier
     [not found] ` <mailman.112.1369106352.22516.help-gnu-emacs@gnu.org>
2013-05-21  5:39   ` Sean McAfee
2013-05-21 12:54     ` Stefan Monnier
2013-05-21 14:23     ` Barry Margolin
2013-05-21 16:38       ` Sean McAfee
2013-05-21 18:06         ` Barry Margolin
2013-05-21 22:43           ` Sean McAfee
2013-05-23  1:01             ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2013-05-21 14:41 Barry OReilly

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.