Damian Conway wrote:
> Mark J. Reed wrote:
>> My understanding is that the P6 way to do that is to return a Capture
>> containing the desired return values (which can lazily do things only
>> when accessed) in the appropriate slots.
>
> Return a Capture or a more heavily overloaded object, depending on how
> fine a degree of context discrimination you need.
>
> Further to that idea, I'm actually planning to attempt to port
> Contextual::Return to Perl 6 in the next few weeks.

Right.  I don't think that a Capture has sufficiently fine context
discrimination to handle even those cases that Perl 5 was able to
handle.  For instance, consider a function that's intended to return a
list of items in list context, or a count of the items in item
context: since imposing item context on a Capture object extracts the
first positional parameter, a Capture object wouldn't be able to be
loaded properly to do this.  So something along the lines of
Contextual::Return is probably a good idea.

Hmm... it might be nice to be able to use something akin to a "given
... when" construct:

    sub foo() {
        return context {
            when Item { 5 }
            when List { a, b, c, d, e }
        }
    }

The idea here is that the 'context' keyword means that the block is to
be called as soon as the desired context is known, with said context
being passed to the block as its topic ('$_').

-- 
Jonathan "Dataweaver" Lang

Reply via email to