<div dir="auto">FWIW I think this is a good change to make.<div dir="auto"><br></div><div dir="auto">frank</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Apr 12, 2017 06:43, "Bert Freudenberg" <<a href="mailto:bert@freudenbergs.de">bert@freudenbergs.de</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi all,<div><br></div><div>there was an interesting question about Generators on StackOverflow:</div><div><br></div><div><a href="http://stackoverflow.com/questions/43340007/yield-prolog-adaptation-for-smalltalk" target="_blank">http://stackoverflow.com/<wbr>questions/43340007/yield-<wbr>prolog-adaptation-for-<wbr>smalltalk</a><br></div><div><br></div><div>It's about a pseudo-Prolog implementation using Generators and yield.</div><div><br></div><div>Our Generator *almost* works for this, except there is a little problem with the timing of side-effects:</div><div><br></div><div><div>| x gen |</div><div>x := 0.</div><div><br></div><div>gen := Generator on: [:g |</div><div><span class="m_-3894763681102661231gmail-Apple-tab-span" style="white-space:pre-wrap">  </span>x := 1.</div><div><span class="m_-3894763681102661231gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>g yield: nil.</div><div><span class="m_-3894763681102661231gmail-Apple-tab-span" style="white-space:pre-wrap"><br></span></div><div><span class="m_-3894763681102661231gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>x := 2.</div><div><span class="m_-3894763681102661231gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>g yield: nil.</div><div><span class="m_-3894763681102661231gmail-Apple-tab-span" style="white-space:pre-wrap"><br></span></div><div><span class="m_-3894763681102661231gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>x := 3.</div><div><span class="m_-3894763681102661231gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>g yield: nil].</div><div><br></div><div>gen do: [:i | Transcript cr; show: x].</div></div><div><br></div><div>This *should* print '1 2 3' but actually prints '2 3 3'. This is because our Generator is written to support a streaming interface (using atEnd and peek) so it always computes one yield ahead.</div><div><br></div><div>In other languages (e.g. JavaScript, Python, C#) this is different, the execution is paused at the current yield statement, not 'before' the next one. In general, a generator cannot know if there will be more "yields" without executing more code. In Javascript, 'next' returns both a value and a 'done' flag, and if the flag is true, the returned value must not be used.</div><div><br></div><div>IMHO we should fix this - see attached changeset Marcel and I came up with. We did not fix the tests yet, but the behavior seems right.</div><div><br></div><div>Even though generators are in the image for a long time they are not used widely yet (AFAIK) so maybe we can still change the behavior? What do people think?</div><div><br></div><div>- Bert & Marcel -</div><div><br></div></div>
<br><br>
<br></blockquote></div></div>