Piers Cawley wrote:

Speaking of multis and constants, Greg McCarroll wondered on IRC if
this would work:

    multi factorial (Int 0) { 1 }
    multi factorial (Int $n) { $n * factorial($n-1) }

Probably not. We did discuss whether multimethods should be able to be overloaded by value, but concluded (for that week, at least ;-) that this
might prove syntactically excessive.


Besides which, since multimethod dispatch will have to use C<isa> to determine type compatibility on parameters anyway, it's trivial to implement this form of value-based dispatch yourself:

     class Zero {
         multi *isa ($obj, Zero $class) { $obj ~~ 0 }
     }

# and then...

multi factorial (Int&Zero $g) { 1 }

or, supposing we have some form of parameterized types, you could create something more generic like:

     class Val($N) {
        multi *isa ($obj, Val($N) $class) { $obj ~~ $N }
     }

# and then...

multi factorial (Int&Val(0) $g) { 1 }


;-)


Damian



Reply via email to