jastrachan | 20 Apr 19:25 2004

Re: Generators take 2 (was Generator Expressions)

On 20 Apr 2004, at 17:57, Galarneau, Neil wrote:
>> -----Original Message-----
>> From: groovy-dev On Behalf Of Mark C. Chu-Carroll
>> Sent: Monday, April 19, 2004 4:41 PM
>> Subject: Re: [groovy-dev] Generator Expressions
>> Anyway - my conclusions, as far as implementations go, is that
>> there are really two choices for how to do generators right. One
>> is thread based, the other isn't.
> My impression is that co-routines need to be based on threads, but
> generators don't.
> The Jcon Implementation uses threads for co-routines but not for
> generators: http://www.cs.arizona.edu/icon/jcon/impl.pdf

Great, thanks for the link.

> I think that the following steps would need to be taken in
> generators to yield values rather than returning them:
> 1) make all variables that are local to the method be private
>      instance or static variables instead (so the state is saved
>      across yields automatically)
> 2) make the code generator not leave things on the stack across
>      a yield (this removes the need to copy & restore the top of
>      the stack)
> 3) introduce a new synthetic private variable which records which
>      yield is about to be executed so that when the method is
>      re-executed the code can jump to the right place to continue
>      execution (so a switch statement or jump table would be the
>      first piece of code executed in a continuation method)
> Are other changes needed that I missed?
> My impression is that #1 is easy, and #3 should be straightforward,
> but I don't know enough about the implementation to comment on #2.
> This implementation would allow the generator to be written in
> a very easy way in Groovy.
> Comments?

Sounds great. It wouldn't be too hard to implement either.

I'm thinking we might wanna create a little nested static class with 
the local variable state inside it (or just use a Map to start with for 
ease of implementation :).

One minor issue might be threading. Would we expect 2 threads to call a 
generator on the same object? If so we could maybe assume that only 1 
generator will be in use by 1 thread at once and use a static 
ThreadLocal to maintain the 'stack' state for the generator. This would 
then allow multiple threads to use possibly different generators in the 
same object concurrently.