all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: <help-gnu-emacs@gnu.org>, "'Tom Roche'" <Tom_Roche@pobox.com>
Subject: RE: [emacs-lisp newbie] print-something() -> clipboard?
Date: Mon, 21 May 2012 06:24:17 -0700	[thread overview]
Message-ID: <6784456F5E1247D387CCC7C99DA86748@us.oracle.com> (raw)
In-Reply-To: <87boli1ttd.fsf@pobox.com>

> > (defun foo (&optional arg)
> >  "..."
> >  (interactive "P")
> >  (let ((bn ...))
> >    (if arg
> >      (insert bn)
> >      (kill-new bn))))
> 
> Thanks! I probably should `let`,

Yes, you should.

> but this works:
> 
> (defun print-buffer-name (&optional arg)
>   "..."
>   (interactive "P")
>   (setq bn ...)
>   (if arg
>     (kill-new bn)
>     (insert bn)))

`bn' is a free variable here - by default a global, dynamically scoped variable.

If you or some code that you use defines another variable named `bn' then your
code will suffer from variable capture: You could end up changing the other
variable.

There is NO reason to use `setq' here, and NO reason not to use `let'.

When you suffer from variable capture you don't necessarily realize it, and
debugging the problem (with perhaps farflung symptoms) can be a headache.  A
self-imposed headache in this case, for no benefit.




      reply	other threads:[~2012-05-21 13:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-18 19:46 [emacs-lisp newbie] print-something() -> clipboard? Tom Roche
2012-05-19  7:19 ` Andreas Röhler
2012-05-20 18:57 ` Tom Roche
2012-05-20 19:20   ` Drew Adams
2012-05-20 20:27 ` Tom Roche
2012-05-21 13:24   ` Drew Adams [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6784456F5E1247D387CCC7C99DA86748@us.oracle.com \
    --to=drew.adams@oracle.com \
    --cc=Tom_Roche@pobox.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.