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

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

Re: Is there a way of setting a variable only when it exists?


From: Tomas Nordin
Subject: Re: Is there a way of setting a variable only when it exists?
Date: Wed, 16 Mar 2022 06:02:59 +0000

Marcin Borkowski <mbork@mbork.pl> writes:

> On 2022-03-14, at 14:48, Emanuel Berg via Users list for the GNU Emacs text 
> editor <help-gnu-emacs@gnu.org> wrote:
>
>> Stefan Monnier via Users list for the GNU Emacs text editor wrote:
>>
>>>> I assume it would be fairly easy to code such a macro
>>>> (using `boundp'), but maybe it exists already?
>>>
>>> AFAIK it doesn't exist yet. The reason for it is that it is
>>> not often useful. Typically there are two cases:
>>>
>>> - If the var exist, you want to set it and if not you have
>>>   no fallback. In that case, it is typically harmless to set
>>>   the var even when it doesn't exist, so the code just uses
>>>   `setq` without bothering to test `boundp`.
>>>
>>> - If the var exist you want to set it, and if it doesn't you
>>>   want to do something else. In that case, the something
>>>   else tends to depend on the specifics so (if (boundp 'foo)
>>>   (setq foo ..) ...) is about s good as it gets.
>>
>> If it exists set it with `setq'. If it doesn't exist, create
>> and set it ... with `setq'?
>
> What if it's an internal Emacs variable which might become a user option
> one day (I submitted a bug report about it) and then my customization
> silently disappears?  It's the "silently" part I want to guard
> against...

What about doing an assert of sorts

(message "some--internal-var is %S" some--internal-var)
(setq some--internal-var 42)

It will crash your init process if some--internal-var is void.

--
Tomas



reply via email to

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