Thomas Sandlaà writes:
> Rod Adams wrote:
> >And then internally dispatch on what is defined and undefined.
>
> Why all that burden if Perl 6 is getting a strong type system,
> that can do the sub selection and compile in dynamic dispatch if
> needed?
>
> I imagine:
>
> multi sub cos( Num +$angle ) returns Num where { -1.0 <= $_ <= 1.0 }
Lose the + on $angle. + indicates a named-only parameter. You want
this to be positional.
> class Radians is Num
> {
>
> }
> class Degrees is Num
> {
> postfix:<Â> ( Num $x ) { return $x * PI / 180 }
You probably shouldn't define this inside the Desgrees class. And you
should probably tell me whether it's a sub, a method, a macro, a
submethod, or a small puppy.
> multi sub cos( Degrees +$angle ) returns Num where { -1.0 <= $_ <= 1.0 }
> {
> return cos( $angle * PI / 180 );
> }
I think that's defining Degrees::cos to be a multi, to be differentiated
from other Degrees::cos'es. But that may be right.
I'd just make it a method.
method cos($angle:) returns Num where { -1 <= $_ < 1 } {
cos($angle * PI / 180);
}
> }
>
> Note that cos(30Â) calls &cos<Num> while &cos<Degrees> is used for
> cases like:
>
> Degrees $angle = 30.0;
>
> if cos $angle > 2.0 { print "HaloO typechecker!" }
That's certainly an interesting way to do it.
> And I still wonder if the ones without 'Â' on their keyboard could
> directly call 'cos<Degrees> $angle'.
&cos<Degrees>($angle) or Degrees::cos($angle), depending on whether you
defined it your way or my way. Uh oh, I thought we were going to make
single-invocant multis and methods the same everywhere...
Luke