From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Oleh Newsgroups: gmane.emacs.help Subject: Re: Any disadvantages of using put/get instead of defvar? Date: Fri, 21 Feb 2014 10:12:44 +0100 Message-ID: References: <87ob218lsu.fsf@thinkpad-t61.fritz.box> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: ger.gmane.org 1392973976 14983 80.91.229.3 (21 Feb 2014 09:12:56 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 21 Feb 2014 09:12:56 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Tassilo Horn Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Feb 21 10:13:04 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WGmAK-0002Wx-P0 for geh-help-gnu-emacs@m.gmane.org; Fri, 21 Feb 2014 10:13:04 +0100 Original-Received: from localhost ([::1]:43222 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGmAK-0002Bh-5q for geh-help-gnu-emacs@m.gmane.org; Fri, 21 Feb 2014 04:13:04 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57802) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGmA4-0002Am-OE for help-gnu-emacs@gnu.org; Fri, 21 Feb 2014 04:12:53 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WGmA3-0001br-Q0 for help-gnu-emacs@gnu.org; Fri, 21 Feb 2014 04:12:48 -0500 Original-Received: from mail-we0-x229.google.com ([2a00:1450:400c:c03::229]:42578) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGmA1-0001bJ-LG; Fri, 21 Feb 2014 04:12:45 -0500 Original-Received: by mail-we0-f169.google.com with SMTP id t61so2364686wes.14 for ; Fri, 21 Feb 2014 01:12:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=6h/J2Q6LtAt1NAk4TxFdaubHKTWnO4D2y2zPcEgcuc8=; b=zZOxZke24yaLP8X5GHuvXIXCcsRcHQa9dIUbWAw6F5gswn9+coEeuAUH64uEL7MByi TPW296qOuDUSg6RSYZGPafLk/k8Zh9WEdrsqQydOCDFxBB7pY407qotW3TA2B0pQS5ve stp5TizMz4yE3FglS6xRjVcj0ZRRduZ8SuGobOiVXqZd2kZQ0pvsI1ax9ZiUyzh5lr+w Wtm+DKHHdLoG63wSYoSKnBZDDmuec9emcg06HvBYXfC2Fxefk4ErZbU57z3HdVU7nU61 7qvucp+h2s7eWUlOKDXBHQNrlg4L55aHk2PJwyqXWYJzMS783fxJzh1UXrWWoPLb0lKN 7qSg== X-Received: by 10.180.96.102 with SMTP id dr6mr2161685wib.19.1392973964757; Fri, 21 Feb 2014 01:12:44 -0800 (PST) Original-Received: by 10.227.147.68 with HTTP; Fri, 21 Feb 2014 01:12:44 -0800 (PST) In-Reply-To: <87ob218lsu.fsf@thinkpad-t61.fritz.box> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c03::229 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:96147 Archived-At: >> 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? I've looked at `(symbol-function 'counter)` and `(byte-compile 'counter)` and it seems that it does. regards, Oleh