<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">http://stackoverflow.com/questions/43340007/yield-prolog-adaptation-for-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="gmail-Apple-tab-span" style="white-space:pre">        </span>x := 1.</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>g yield: nil.</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"><br></span></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">       </span>x := 2.</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>g yield: nil.</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"><br></span></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">       </span>x := 3.</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </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>