develooper Front page | perl.perl6.language | Postings from February 2005

How are types related to classes and roles?

Thread Next
Thomas Sandlaß
February 23, 2005 09:21
How are types related to classes and roles?
Message ID:

I'm very puzzled about what is meant by type and class in Perl6.
In the sort ruling
we saw a system of types that allow to nicely dispatch into different version
of &sort. OTOH every class or role name can serve the same purpose.
Built-in value types like Str and Int seem to be auto-coercive instead of
MMD, in particular when playing together with dedicated operators like
binary '+' versus '~'. And how does all this combine with the notion of context?
I guess that basically influences method selection.

Take the following example:

# part 1
class Cyclic[uint $base] is Int
    multi *postfix:<++> ( Cyclic[uint $base] $i )
       returns Cyclic[uint $base]
         return (($i as uint) + 1) % $base;
    multi *infix:<+> ( Cyclic[uint $base] $x, Int $y )
       returns Cyclic[uint $base]
       is symmetric
         return abs( ($x as uint) + $y ) % $base;

# part 2
sub foo( Int $begin, Int $end )
    while $begin < $end
       $begin++; # assumes Int axiom: $i + 1 > $i

#part 3
my Cyclic[8] $cyclic = 3;

foo( $cyclic, 10 ); # never returns?

Does the design intent to catch such "surprises"?
E.g. by giving an error at the line 'class Cyclic[uint $base] is Int'?
But how should the violation of an abstract property of Int be checked
by the compiler?
Can this just be remedied by source code review and human reasoning?
Is there a chance for designers of classes like Int to prevent method
overriding which violates some constraints?
If yes, how are these constraints specified?
my Cyclic[7] enum DayOfWeek <Mon Tue Wed Thu Fri Sat Sun>;

Another thing: does the above definition work together with enums?

my Cyclic[7] enum DayOfWeek <Mon Tue Wed Thu Fri Sat Sun>;
my DayOfWeek $day = Fri;
say "Fri = {$day}, Sat = {$day + 1}, Sun = {$day + 2}, Mon = {$day + 3}";

Does that say "Fri = Fri, Sat = Sat, Sun = Sun, Mon = Mon"?

TSa (Thomas Sandlaß)

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About