[Top][All Lists]

[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: Fri, 21 Feb 2014 10:39:30 +0100
User-agent: Gnus/5.13001 (Ma Gnus v0.10) Emacs/24.3.50 (gnu/linux)

Oleh <address@hidden> 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)))
> Thanks, Tassilo,
> But doesn't `defvar` introduce overhead this way?

Well, I've measured my counter above versus a version using symbol
properties as you suggest:

  (defun bar ()
    (let ((foo (or (get 'bar 'foo) 1)))
      (put 'bar 'foo (1+ foo))))

My counter is way faster although it uses defvar and setq-local, so that
overhead is still small compared to looking up/putting a symbol


reply via email to

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