Aaron Sherman wrote:
This bit comes from the p6i list, and I just thought I'd ask those
in-the-know if my suggested "returntype" role/property would make sense
here, or if there's another way to do it that makes more sense?

For that matter, does MMD on return type map into Perl6's gestalt at
all, or would it be tumorous?

To sum up:

Dan: return continuations and MMD calling means we can dispatch return
via MMD

Aaron: suggested an implementation in Perl and Python. In Perl, it would
be a role/propery called "returntype" that would take some sort of list
or hash of types and closures. The difference between this and a
given/when block being that the MMD internals of parrot handle which
closure is invoked based on the return type. Thus, very low overhead.

My example in the attached message is mostly error handling, but that's
obviously not the only way it could be used....
[snip]
        $sock.peername()
                does returntype(
                        Net::Address::IP -> $ip {
                                die "Remote host unresolvable: '$ip'";
                        }, Net::Address -> $addr {
                                die "Non IP unresolvable address: '$addr'";
                        }, Str -> $_ {
                                print "Seemingly valid hostname: '$_'\n";
                        });

Could this just work with the switch statement somehow? If not, could 'when' be extended to take a signature that is matched against the return 'arguments'?


        given $sock.peername() {
                when(Net::Address::IP){
                        die "Remote host unresolvable: '$_'"
                }
                when(Net::Address){
                        die "Non IP unresolvable address: '$_'"
                }
                when(Str){
                        print "Seemingly valid hostname: '$_'\n"
                }
        }

or more complexly

        sub foo {
                return time if time % 2;
                return (time,
                        () = gmtime)# ya, I know p6 has a better way,
                                    # but I can't remember it right now
        }
        given foo(){
                when(Int){
                        say $_, " ", join " ", gmtime $_
                }
                when(Int $t, Array @gmt){
                        say $t, " ", join " ", @gmt
                }
        }

Dan

Reply via email to