Will Coleda wrote:
Given the current :method and .yield implementations, the following code:
$ cat foo.pir
.sub main :main
$P1 = newclass 'foo'
$P2 = new 'foo'
$S1 = $P2.'bork'()
say $S1
$P3 = new 'foo'
$S1 = $P3.'bork'()
say $S1
.end
.namespace [ 'foo']
.sub bork :method
$I0 = 0
loop:
.yield($I0)
inc $I0
goto loop
.end
Generates the following output:
$ ./parrot foo.pir
0
1
Which is unsurprising given the current implementation. Is this desired
behavior, though?
It's assuming that methods have no state from one execution to the next,
which isn't a valid assumption once we throw coroutines into the mix.
The output does reflect the desired behavior for class methods, and I've
used it to implement a unique id generator that spans all instances of
the class. But for instance methods, it doesn't make any sense. Of
course, Parrot can't really distinguish class methods from instance
methods yet.
I can work around this by storing an attribute on the class which keeps
my counter in the instance and then retrieving it with getattribute in
the loop, insuring that the state is pulled from the object instead of
the registers in the coroutine/method whenever the coroutine resumes.
For the moment you'll need a hack something like this.
Or we could have yield somehow be instance specific.
If we take a ruby-esque meta-model where the class is an object (in
addition to instances being objects) then the conceptually simplest way
to handle this is to make yield always be object-specific within
methods, whether the object is a class or an instance of a class.
Allison