[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: Andreas Röhler
Subject: Re: Any disadvantages of using put/get instead of defvar?
Date: Fri, 21 Feb 2014 10:51:40 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.2.0

Am 21.02.2014 10:39, schrieb Tassilo Horn:
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


     (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

BTW in earlier times a "let" was used.
IIUC the way to make a function-local value now is "defvar" inside?

reply via email to

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