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: Tassilo Horn
Subject: Re: Any disadvantages of using put/get instead of defvar?
Date: Thu, 20 Feb 2014 23:24:33 +0100
User-agent: Gnus/5.13001 (Ma Gnus v0.10) Emacs/24.3.50 (gnu/linux)

Oleh <ohwoeowho@gmail.com> writes:

> The situation is that I have a function that uses one global variable.
> It's for sure that no other function will want this variable.  In an
> effort to have all code in one place I want to move from:
>
>     (defvar bar-foo 1)
>     (defun bar ()
>       ;; use bar-foo here
>       )
>
> to:
>
>     (defun bar ()
>       (let ((foo (or (get 'bar 'foo) 1)))
>         ;; use foo here
>         ))
>
> So the advantage is that I can move and rename the function without
> worry that the function/variable coupling will break, because now
> everything is inside one function.

You could also define the variable inside the function, i.e., that's a
buffer-local counter:

  (defun counter ()
    (defvar counter-var 1)
    (setq-local counter-var (1+ counter-var)))

Bye,
Tassilo



reply via email to

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