On Wed, 2005-05-04 at 10:07, Aaron Sherman wrote:
> On Wed, 2005-05-04 at 09:47, Joshua Gatcomb wrote:
> > So without asking for S17 in its entirety to be written, is it
> > possible to get a synopsis of how p6 will do coroutines?
> A coroutine is just a functional unit that can be re-started after a
> previous return, so I would expect that in Perl, a coroutine would be
> defined by the use of a variant of return

Oh, I failed to comment on a few things (and given Luke's responses,
this answer can be seen to dove-tail into what he said, though I don't
think you need a coroutine trait most of the time).

Here they are as questions with my expected default answers:

Q: What does a coroutine return when exausted?
A: It either explicitly "return"s something or falls off the end. This
allows you to:

        sub ten() { for 1..10 -> $_ { coreturn $_ } return undef; }

correctly terminating ten() when called in a loop.

If you ever call coreturn, then dropping off the end of the routine
probably implicitly returns undef rather than the last statement
executed as normal. Why? As a default way to signal the caller that
we're done. More sophisticated means (adding traits to the undef) might
be employed.

Q: What happens if you change parameters?
A: Nothing. You would have to store the information about what
parameters were active somewhere, and no matter where you choose
(current lexical pad, parameters, coroutine itself, etc.), there are
many cases that are non-obvious to the programmer, and this gets

        while someco(@x) -> $_ {
                while someco(@x) -> $_ {...}

If you want to modify behavior based on parameter, return a closure that
is a coroutine:

        sub someco(@p) { return -> { for @p -> $_ { coreturn $_ } } }

        my $co1 = someco(@x);
        while $co1.() -> $_ {
                my $co2 = someco(@x);
                while $co2.() -> $_ {...}

Aaron Sherman <[EMAIL PROTECTED]>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback

Reply via email to