2

I tested two similar generators, and the result confused me.

  1. This iterator yields values as I expected:

    ELISP> (iter-defun f (x)
             (setq x (iter-yield (1+  x)))
             (setq x (iter-yield (* 2 x))))
    f
    ELISP> (progn
             (setq x (f 1))
             `(,(iter-next x) ,(iter-next x 3)))
    (2 6)
    
  2. But this one yields a different value:

    ELISP> (iter-defun g (x)
             (setq x (iter-yield (1+  x))
                   x (iter-yield (* 2 x))))
    g
    ELISP> (progn
             (setq x (g 1))
             `(,(iter-next x) ,(iter-next x 3)))
    (2 2)
    

The above two generators are very similar,
but why are the values yielded by their iterators different?

Segrece
  • 83
  • 6
shynur
  • 4,065
  • 1
  • 3
  • 23
  • 1
    I haven't tested to make sure, but my assumption is that in the sequential assignments in the first generator, the x on the RHS of the second assignment is the result of the first assignment, whereas in the second generator the x on the RHS of both assignments is the value of the argument. – NickD Mar 09 '23 at 13:01
  • @NickD: Is it a bug? – shynur Mar 10 '23 at 04:09
  • Not that I know of, but I don't even know if my assumption is correct: I stil have not tried it out. – NickD Mar 10 '23 at 13:26
  • In Emacs 29, the examples yield the same values. Indeed, in Emacs 28 the yielded values are different, like in your question. – dalanicolai Mar 12 '23 at 08:19
  • @dalanicolai: Oh sorry, I forgot to say my Emacs version and machine environment. Yes, my Emacs is v28.2 and the system-configure is x86_64-w64-mingw32. – shynur Mar 12 '23 at 08:27
  • Ah well, we're not reporting bugs here (where this should be mentioned, which happens automatically if you use `M-x report-emacs-bug). I would also not have expected this. Just I noticed that the behavior was different here on version 29, so then I checked on 28. – dalanicolai Mar 12 '23 at 08:39
  • Possibly annoying comment: this is an example of why seasoned coders *never* reassign. The recommended rule is: treat each variable as read-only after initial assignment. For best safety, always do that single initial assignment immediately on introducing the variable. You automatically prevent a whole class of bugs thereby. – Phil Hudson Mar 14 '23 at 20:41
  • @PhilHudson: Reasonable, but not annoying to me because we are talking about different topics here. Your suggestion is about coding conventions; my question is about the inconsistency between doc and actual behaviors. As a novice, when encountering a potential “document bug”, instead of seeing it as a bug, I first wonder if I'm misinterpreting the doc or actual behavior of `setq` or `generator.el`. This inconsistency will make new Emacs users find it hard to study Elisp through the manual, so I post this question. (The way I learned the language is to write some code and watch how they behave. – shynur Mar 15 '23 at 00:09
  • @Shynur: Quite right. In this case the reassignment is key to illustrating the point you're bringing up. I shouldn't have mentioned the coding technique consideration. In this context it's a distraction. – Phil Hudson Mar 16 '23 at 07:56

0 Answers0