Are coroutines broken? (or is this some new/different behavior?)

Andrew Whitworth wknight8111 at gmail.com
Sat Apr 21 10:48:08 UTC 2012


Techically not "broken", though it is behavior I've never quite agreed
with. When a coroutine returns, it is considered "dead" and cannot be
executed again. This makes them, I think, quite useless for most
possible applications.

I thought there was a way to manually reset the coroutine to get it
invokable again. However, looking at the code I don't see any such
thing.

At the moment, the best and only real use for a coroutine is to use it
as a self-contained infinite loop for spitting out constants and
counters:

function my_coroutine() {
   var my_value = ....;
   while(true) yield my_value;
}

If people want more behaviors out of coroutine than that small example
allows, I suggest we change the way they work.

--Andrew Whitworth


On Sat, Apr 21, 2012 at 4:57 AM, Alvis Yardley <ac.yardley at gmail.com> wrote:
>
> While working through some of Whiteknight's documentation on Winxed, at
> 'http://whiteknight.github.com/Rosella/winxed/functions.html', I tried the
> below bit of winxed code:
>
> function MyCoroutine() {
>    yield 1;
>    yield 2;
>    yield 3;
>    return 4;
> }
>
> function main[main]() {
>    for (int i = 0; i < 8; i++)
>        say(MyCoroutine());
> }
>
> Which resulted in the following:
>
> 1
> 2
> 3
> 4
> Cannot resume dead coroutine.
>
> I then tried the following pir code, found in
> 'docs/book/pir/ch06_subroutines.pod':
>
> .sub 'MyCoroutine'
>    .yield(1)
>    .yield(2)
>    .yield(3)
>    .return(4)
> .end
>
> .sub 'main' :main
>    $I0 = MyCoroutine() #1
>    say $I0
>    $I0 = MyCoroutine() #2
>    say $I0
>    $I0 = MyCoroutine() #3
>    say $I0
>    $I0 = MyCoroutine() #4
>    say $I0
>    $I0 = MyCoroutine() #1
>    say $I0
>    $I0 = MyCoroutine() #2
>    say $I0
>    $I0 = MyCoroutine() #3
>    say $I0
>    $I0 = MyCoroutine() #4
>    say $I0
> .end
>
> Which similarly resulted in the following:
>
> 1
> 2
> 3
> 4
> Cannot resume dead coroutine.
> current instr.: 'main' pc 35 (mycoroutine.pir:11)
>
> So, are coroutines broken or is this some new/different behavior?
>
> I'd appreciate any assistance or guidance.
>
> Thank you.
>
> --
> Alvis
> _______________________________________________
> http://lists.parrot.org/mailman/listinfo/parrot-dev


More information about the parrot-dev mailing list