Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return context sensitive return values in Perl 6?

Tags:

perl

raku

In the summary of differences between Perl 5 and Perl 6, it is noted that the wantarray function is gone:

wantarray() is gone

wantarray is gone. In Perl 6, context flows outwards, which means that a routine does not know which context it is in.

Instead you should return objects that do the right thing in every context.

Could someone provide an example of how such an object is created?

like image 522
Eric Strom Avatar asked Oct 18 '10 20:10

Eric Strom


1 Answers

I think 2 examples might be:


http://perlcabal.org/syn/S13.html#Type_Casting

A class may define methods that allow it to respond as if it were a routine, array, or hash. The long forms are as follows:

method postcircumfix:<( )> ($capture) {...}
method postcircumfix:<[ ]> (**@slice) {...}
method postcircumfix:<{ }> (**@slice) {...}

Those are a bit unwieldy, so you may also use these short forms:

method &.( $capture ) {...}
method @.[ **@slice ] {...}
method %.{ **@slice } {...}

Also, I think this might be relevant though less so: http://perlcabal.org/syn/S12.html

Search for:

You may write your own accessors to override any or all of the autogenerated ones.

So you return an object which has several context-specific accessors.


Interestingly enough, it started out with Perl6 replacing "wantarray" with a generic "want": RFC 98 (v1) context-based method overloading, circa 2000, also at http://dev.perl.org/perl6/rfc/21.html . I'm not sure why/when the change was made.

like image 151
DVK Avatar answered Sep 19 '22 15:09

DVK