help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Understanding the "let" construct and the setting of variables


From: Joost Kremers
Subject: Re: Understanding the "let" construct and the setting of variables
Date: Thu, 17 Dec 2020 09:56:12 +0100
User-agent: mu4e 1.5.7; emacs 27.1.50

On Thu, Dec 17 2020, steve-humphreys@gmx.com wrote:
> This gets me to "defvar".  I have read that "setq" does net actually
> make a variable.  It is defvar that makes a variable available.

`defvar` creates a global variable, yes. `setq` creates a global variable if the
variable does not exist yet. So if you do:

```
(let ((a (some-value)))
  (do-something)
  (setq a (some-value-based-on a))
  (do-something-else))
```

Then setq updates the local binding for `a`. If you use setq on a variable that
doesn't have a local binding (i.e., isn't bound in the let that contains the
setq), it creates a new, global binding.

> Do the question really is this.  What happens when one sets an object with 
> "setq"
> if elisp does not make a variable from the name?

Not sure I understand the question. With `setq`, two things can happen: either
there is already a binding for the variable, in which case `setq` updates the
binding (assigns a new value to the existing variable), or there is no such
binding (i.e., the variable doesn't exist), and in that case the variable is 
created.

-- 
Joost Kremers
Life has its moments



reply via email to

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