develooper Front page | perl.perl6.language | Postings from September 2003

Re: Dispatch on context

Thread Previous
From:
Luke Palmer
Date:
September 8, 2003 01:39
Subject:
Re: Dispatch on context
Message ID:
20030908083832.GA15866@babylonia.flatirons.org
Piers Cawley writes:
> So, I was wondering about how to do multidispatch based on the
> context in which something is called, which leads me to wonder if
> there's going to be a way of reifying calling context so I could
> write:
> 
>   method whatever($arg1, $arg2) 
>     { my multi whatever ($self, Scalar $context:
>                          $arg1, $arg2) { ... }
>       my multi whatever ($self, Array $context: 
>                          $arg1, $arg2) { ... }
>       whatever($_, want(), $arg1, $arg2) }

The way I see that happening is if we allow certain kinds of constants
in multi declarations.

    my multi whatever ($self, Class $context is derived(Scalar):
                       $arg1, $arg2) {...}

That'd be nice in a number of different ways.  Without this special
feature, an enabling level of indirection can be added with
parameterized types:

    my class ClassVal [Class $TYPE] {
        # Does nothing, and may even be optimized away
    }
    my multi whatever ($self, ClassVal[Scalar] $context:
                       $arg1, $arg2) {...}
    # ...

    whatever($_, ClassVal[want], $arg1, $arg2);
    #                          ^ does a : go here?

That doesn't handle inheritance correctly, but it can be made to,
provided proper introspection into $TYPE. 

Luke

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About