On 8/10/05, TSa <[EMAIL PROTECTED]> wrote:
> Here is an example of a 2D distance method
> 
>    role Point
>    {
>      has Num $.x;
>      has Num $.y;
>    }
>    method distance( Point $a, Point $b --> Num )
>    {
>       return sqrt( ($a.x - $b.x)**2 - ($a.y - $b.y)**2);
>    }
> 
> Now comes the not-yet-Perl6 part:

I wouldn't say that...

>    Complex does Point where { $.x := $.real; $.y := $.imag }

use self './';

Complex does role {
    does Point;
    method x () is rw { ./real }
    method y () is rw { ./imag }
}

>    Array[Num] does Point
>      where { $.size >= 2 or fail;
>              $.x := @.values[0]; $y := @.values[1] }

# This one is quite a bit more dubious than the last
Array[Num] where { .elems >= 2 } does role {
    does Point;
    method x () is rw { ./[0] }
    method y () is rw { ./[1] }
}

That subtype would have to be an "auto-subtype", that is, it would
have to know that it is a member of that type without being told so. 
Such subtypes can introduce major inefficiencies in the program, and
are best left either out of the language, or somewhere in the dark
depths of I-know-what-I'm-doing-land.

Luke

Reply via email to