unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: <tomas@tuxteam.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Alist remaining empty
Date: Sat, 28 Sep 2024 07:26:54 +0200	[thread overview]
Message-ID: <ZveTnlsqxOYLQ26D@tuxteam.de> (raw)
In-Reply-To: <E3MPkc69Uwger2jsCJJVFTowlV220mKY3r91hU64J4PvgrLT__z0Vj8e_2YaQZGlQ0puyX75PzplwCpW2SBqibhjZfEzt5iTKYwi9FiDEi4=@protonmail.com>

[-- Attachment #1: Type: text/plain, Size: 2423 bytes --]

On Fri, Sep 27, 2024 at 10:50:21PM +0000, Heime wrote:
> I have alist variable warn-alist, which I set to nil.  I am supposed
> to push some alist examples if the values have not been added yet.
> 
> But I find that with the following function, the warn-alist remains empty.

As it should. Please, please: learn about variables, variable scopes,
values. Otherwise, you're bound to stay in an endless loop.

I intersperse comments into your code:

;; Somewhere "outside", warn-alist has been defined. Or not.
;; We just don't know.

> (defun warn-test ()
> 
>   (interactive)
> 
>   (setq warn-alist nil)

;; Here, its value is set to nil (whatever its value was before).
;; Is it global? Buffer-local? Lexical? We don't know. We don't
;; know either whether it matters.
> 
>   (let ( (qwpanel warn-alist)

;; A new (probably lexical, but possibly dynamic) binding for
;; qwpanel is introduced: its value is set to the value of
;; warn-alist -- which is nil (see above)
> 
>          ;; Init flag to track if values have been added
>          (already-added nil) )
> 
>     ;; If the alist is empty, add the cons data
>     (unless qwpanel
>       (setq already-added t))

;; Things are done in a somewhat roundabout way. In the conditional
;; above, we /know/ that qwpanel is nil, so already-added stays nil
> 
>     (unless already-added

;; this means we could replace here "unless already-added" by
;; "unless qwpanel", and then completely do away with it (a smart
;; compiler is going to do that for you anyway).
>       (push (cons 'ques-string "Warning Message")   qwpanel)
>       (push (cons 'ques-number 42)                  qwpanel)
>       (push (cons 'ques-symbol 'example-symbol)     qwpanel)
>       (push (cons 'ques-list '(1 2 3))              qwpanel)
>       (push (cons 'ques-boolean t)                  qwpanel)
>       (push (cons 'ques-nil nil)                    qwpanel)
>       (push (cons 'ques-cons (cons 'nested "cell")) qwpanel)
>       (push (cons 'ques-function #'print)           qwpanel)) ))

;; and now, when leaving the (let ...) scope, you throw away your
;; qwpanel binding, and all of its value, for the garbage collector
;; to feast on it. Viewed "from the outside", which you haven't
;; shown to us, nothing, except possibly setting warn-alist to nil
;; has happened.

Working as designed, I'd say.

Cheers
-- 
tomás

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

  reply	other threads:[~2024-09-28  5:26 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-27 22:50 Alist remaining empty Heime
2024-09-28  5:26 ` tomas [this message]
2024-09-28  9:11   ` Heime
2024-09-28  9:14     ` tomas
2024-09-28 15:22       ` Heime
2024-09-28 15:16 ` Stefan Monnier via Users list for the GNU Emacs text editor

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=ZveTnlsqxOYLQ26D@tuxteam.de \
    --to=tomas@tuxteam.de \
    --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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).