unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Alist remaining empty
@ 2024-09-27 22:50 Heime
  2024-09-28  5:26 ` tomas
  2024-09-28 15:16 ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 6+ messages in thread
From: Heime @ 2024-09-27 22:50 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor

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.

(defun warn-test ()

  (interactive)

  (setq warn-alist nil)

  (let ( (qwpanel warn-alist)

         ;; 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))

    (unless already-added
      (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)) ))





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Alist remaining empty
  2024-09-27 22:50 Alist remaining empty Heime
@ 2024-09-28  5:26 ` tomas
  2024-09-28  9:11   ` Heime
  2024-09-28 15:16 ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 6+ messages in thread
From: tomas @ 2024-09-28  5:26 UTC (permalink / raw)
  To: help-gnu-emacs

[-- 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 --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Alist remaining empty
  2024-09-28  5:26 ` tomas
@ 2024-09-28  9:11   ` Heime
  2024-09-28  9:14     ` tomas
  0 siblings, 1 reply; 6+ messages in thread
From: Heime @ 2024-09-28  9:11 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs

On Saturday, September 28th, 2024 at 5:26 PM, tomas@tuxteam.de <tomas@tuxteam.de> wrote:

> 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)

I set the global to nil so that the push upon qwpanel will execute on
first call.  
 
> ;; 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).

Right, agreed
 
> > (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.

Inside the let scope I call the following function with qwpanel
 
(defun alcons (alist &optional bfrn)
  "Print Associateed List ALIST to the buffer BFRN."

  (let* ( (bfname (or bfrn "ALIST Cons"))
          (dbuffer (get-buffer-create (concat "𒆳 " bfname))) )

    (with-current-buffer dbuffer
      (face-remap-add-relative 'default :background "blue")

      (insert "\n ALIST Cons \n\n")

      (dolist (entry alist)
        (insert (format " %S\n" entry)))) ))

;; --------------------------------------------------------------------

(defun warn-test ()

  (setq warn-alist nil)

  (let ( (qwpanel warn-alist) )
    (unless qwpanel
      (push (cons 'ques-string "Warning Message") qwpanel)
      (push (cons 'ques-number 42) qwpanel))
    (alcons qwpanel "Alist Cons Test")) )


> Working as designed, I'd say.
> 
> Cheers
> --
> tomás



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Alist remaining empty
  2024-09-28  9:11   ` Heime
@ 2024-09-28  9:14     ` tomas
  2024-09-28 15:22       ` Heime
  0 siblings, 1 reply; 6+ messages in thread
From: tomas @ 2024-09-28  9:14 UTC (permalink / raw)
  To: Heime; +Cc: help-gnu-emacs

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

On Sat, Sep 28, 2024 at 09:11:36AM +0000, Heime wrote:

[...]

> Inside the let scope I call the following function with qwpanel

Given your answer, I don't even dare a guess whether we've
understood each other or not.

I'm out of ideas.

Cheers
-- 
t

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Alist remaining empty
  2024-09-27 22:50 Alist remaining empty Heime
  2024-09-28  5:26 ` tomas
@ 2024-09-28 15:16 ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 6+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2024-09-28 15:16 UTC (permalink / raw)
  To: help-gnu-emacs

> 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.

Have you gone through the Introduction to Emacs Lisp manual that's bundled
with Emacs (Help => More Manuals => Introduction to Emacs Lisp)?


        Stefan




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Alist remaining empty
  2024-09-28  9:14     ` tomas
@ 2024-09-28 15:22       ` Heime
  0 siblings, 0 replies; 6+ messages in thread
From: Heime @ 2024-09-28 15:22 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs






Sent with Proton Mail secure email.

On Saturday, September 28th, 2024 at 9:14 PM, tomas@tuxteam.de <tomas@tuxteam.de> wrote:

> On Sat, Sep 28, 2024 at 09:11:36AM +0000, Heime wrote:
> 
> [...]
> 
> > Inside the let scope I call the following function with qwpanel
> 
> 
> Given your answer, I don't even dare a guess whether we've
> understood each other or not.
> 
> I'm out of ideas.

I have now successfully resolved all the problems. By removing the global 
variable and initializing the lexical local variable to nil, things became 
more straightforward.  Eliminating the already-added flag further simplified 
the process, allowing me to focus on the core functionality.
 
> Cheers
> --
> t



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-09-28 15:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-27 22:50 Alist remaining empty Heime
2024-09-28  5:26 ` tomas
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

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).