[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [emacs-lisp newbie] print-something() -> clipboard?
From: |
Drew Adams |
Subject: |
RE: [emacs-lisp newbie] print-something() -> clipboard? |
Date: |
Mon, 21 May 2012 06:24:17 -0700 |
> > (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.