[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Understanding the "let" construct and the setting of variables
From: |
Emanuel Berg |
Subject: |
Re: Understanding the "let" construct and the setting of variables |
Date: |
Sat, 19 Dec 2020 07:06:48 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
steve-humphreys wrote:
> Here the variable remains local. my-var does not become
> a global variable because it is defined within the let.
> So one can use "set" with variables defined in a "let"
> construct. I thought you could not do that - call "setq" on
> a local variable defined in a "let" expression.
But often it isn't needed.
>> (defun my-fun ()
>> (let ((my-var nil)))
>> (setq my-var 2))
>>
>> (my-fun)
>>
>> my-var is here defined as 2 and became global variable.
>>
>> And each time that variable is already defined with `defvar' one
>> can then change it with setq.
>
> And here "my-var" becames a global variable because my-var
> is set using "setq" outside the "let" expression.
Yes, but here lies the problem and danger with using `setq'
within `let/let*', because that will also create a global
variable if it isn't in the let varlist already.
Everything is global in Elisp BTW except the stuff you'll find
for example within special forms like `let'. That's another
reason you ought to use them as often as you can, to
encapsulate all the little pieces, to hide the machinery from
the rest of the world and only present a clean and
easily defined/understood interface.
f(x) = y
so tell everyone, give f x and you'll get y. But how that
happens, how y is computed from x, is f's business and there
is no need, actually it should be avoided, there is no need to
clutter the global space with stuff that only concerns f.
Note that this mathematical example doesn't have to be math.
It can be a Quake game, or whatever. Quake, which is based on
linear algebra and spatial geometry. OK, bad example.
--
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal
- Understanding the "let" construct and the setting of variables, steve-humphreys, 2020/12/16
- Re: Understanding the "let" construct and the setting of variables, Jean Louis, 2020/12/16
- Re: Understanding the "let" construct and the setting of variables, steve-humphreys, 2020/12/17
- Re: Understanding the "let" construct and the setting of variables, Emanuel Berg, 2020/12/19
- Re: Understanding the "let" construct and the setting of variables, Jean Louis, 2020/12/19
- Re: Understanding the "let" construct and the setting of variables, Emanuel Berg, 2020/12/20
Re: Understanding the "let" construct and the setting of variables, Emanuel Berg, 2020/12/18
Re: Understanding the "let" construct and the setting of variables, Joost Kremers, 2020/12/16