unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Weird let behavior (and where to ask for support)
@ 2022-01-23  0:33 dalanicolai
  0 siblings, 0 replies; 4+ messages in thread
From: dalanicolai @ 2022-01-23  0:33 UTC (permalink / raw)
  To: Emacs Devel

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

I am trying again to improve/clean up the implementation of continuous
scroll in doc-view.
 But I find some strange behavior, for which I have no explanation.

 So let me first explain the essence of the problem. I am using the
following code, in a file with lexical-binding enabled (in case that
would matter).
I will provide the full function later also, but here is the essence:

 (defun book-image-positions (image-sizes)
  (let ((positions '(0)))
    (message "POS %s" positions)))

I really use the variable name 'positions' only within this function
(I bind the result to
a variable named 'image-positions'

I would expect that 'positions' would always get initialized as '(0).
But when I print the value after the let, then it shows me a large list.
I guess I am missing something here, and also I guess you probably
know what it is.

A little more context, I have a list with image-sizes (heights), and I
am creating
a list with image positions from it.  For that I am using the following function
(incl. quite some print forms for debugging).

 (defun book-image-positions (image-sizes)
  (let ((sum 0)
        (positions '(0)))
    (print sum)
    (message "POS %s" positions)
    (dolist (s image-sizes)
      (setq sum (+ sum (cdr s)))
      (print (push sum positions)))
    (message "RES %s" (reverse positions))
    (nreverse positions)))

this function gets called multiple times and 'positions' does have the
'(0) value the first time.

Then finally, I was trying to search for some related issue in the
devel archive, but the
 results always get  'cut off', because there are too many search
results. Also, I think
 users will more likely be able to find the question and answer on
stack overflow, so
would you agree it is often best to ask these kind of 'elisp' questions there?

[-- Attachment #2: Type: text/html, Size: 2538 bytes --]

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

* Re: Weird let behavior (and where to ask for support)
@ 2022-01-23  2:51 Pierpaolo Bernardi
  2022-01-23  4:27 ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Pierpaolo Bernardi @ 2022-01-23  2:51 UTC (permalink / raw)
  To: dalanicolai, Emacs Devel

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

You are modifying the constant '(0). Modifying constants has an unspecified effect, one manifestation of which you are observing.

Change '(0) to (list 0).


Il giorno 23 gennaio 2022, alle ore 01:34, dalanicolai <dalanicolai@gmail.com> ha scritto:

I am trying again to improve/clean up the implementation of continuous scroll in doc-view.
 But I find some strange behavior, for which I have no explanation.

 So let me first explain the essence of the problem. I am using the following code, in a file with lexical-binding enabled (in case that would matter).
I will provide the full function later also, but here is the essence:

 (defun book-image-positions (image-sizes)
  (let ((positions '(0)))
    (message "POS %s" positions)))

I really use the variable name 'positions' only within this function (I bind the result to
a variable named 'image-positions'
I would expect that 'positions' would always get initialized as '(0).
But when I print the value after the let, then it shows me a large list.
I guess I am missing something here, and also I guess you probably 
know what it is.

A little more context, I have a list with image-sizes (heights), and I am creating
a list with image positions from it.  For that I am using the following function
(incl. quite some print forms for debugging).

 (defun book-image-positions (image-sizes)
  (let ((sum 0)
        (positions '(0)))
    (print sum)
    (message "POS %s" positions)
    (dolist (s image-sizes)
      (setq sum (+ sum (cdr s)))
      (print (push sum positions)))
    (message "RES %s" (reverse positions))
    (nreverse positions)))

this function gets called multiple times and 'positions' does have the '(0) value the first time.

Then finally, I was trying to search for some related issue in the devel archive, but the
results always get 'cut off', because there are too many search results. Also, I think
users will more likely be able to find the question and answer on stack overflow, so
would you agree it is often best to ask these kind of 'elisp' questions there?


[-- Attachment #2: Type: text/html, Size: 2953 bytes --]

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

* Re: Weird let behavior (and where to ask for support)
  2022-01-23  2:51 Weird let behavior (and where to ask for support) Pierpaolo Bernardi
@ 2022-01-23  4:27 ` Stefan Monnier
  2022-01-23 10:26   ` dalanicolai
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2022-01-23  4:27 UTC (permalink / raw)
  To: Pierpaolo Bernardi; +Cc: dalanicolai, Emacs Devel

> You are modifying the constant '(0). Modifying constants has an unspecified
> effect, one manifestation of which you are observing.
> Change '(0) to (list 0).

Exactly.  IOW never use `nreverse`, `sort`¸ `nconc`, etc... on a list
unless you know for sure all the `conses` in that list are all freshly
made and not shared with anything else.

    '(0)

does not return a freshly made list, but instead return the same old
and tired list created when the code was loaded into Emacs.


        Stefan




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

* Re: Weird let behavior (and where to ask for support)
  2022-01-23  4:27 ` Stefan Monnier
@ 2022-01-23 10:26   ` dalanicolai
  0 siblings, 0 replies; 4+ messages in thread
From: dalanicolai @ 2022-01-23 10:26 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Pierpaolo Bernardi, Emacs Devel

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

Thanks for the answers. I must admit that I did not study the full elisp
manual (far from it),
and also I did not understand your answers immediately. Anyway, now that I
knew what
to look for I have found the section about 'self evaluating forms'
<https://www.gnu.org/software/emacs/manual/html_node/elisp/Self_002dEvaluating-Forms.html>
in the elisp manual and
reread the quote and mutability sections. I think it is clear now...

[-- Attachment #2: Type: text/html, Size: 518 bytes --]

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

end of thread, other threads:[~2022-01-23 10:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-23  2:51 Weird let behavior (and where to ask for support) Pierpaolo Bernardi
2022-01-23  4:27 ` Stefan Monnier
2022-01-23 10:26   ` dalanicolai
  -- strict thread matches above, loose matches on Subject: below --
2022-01-23  0:33 dalanicolai

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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