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

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

Re: Any disadvantages of using put/get instead of defvar?


From: Stefan Monnier
Subject: Re: Any disadvantages of using put/get instead of defvar?
Date: Fri, 21 Feb 2014 09:41:00 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

> Sure (I guess you mean with the defvar before the defun), but Oleh's
> goal was not to have the variable and the function separated.

If he wants to keep state between invocations of the function, then that
state has to be external (logically) to the function.  So it is best to
reflect this syntactically by making the variable external as well.

If you want it syntactically "closer", you can put it between the
function's name and the function itself (so it's still outside the
function, but at the same time, it's still within the overall
definition):

  (defalias 'counter
    (let ((counter 1))
      (lambda () (setq counter (1+ counter)))))

Of course, this relies on lexical-binding and is not buffer-local.


        Stefan



reply via email to

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