P is for Practical  
PerlMonks 
Something that bugs me about the Numeric class hierarchy in Perl 6by grondilu (Friar) 
on Nov 24, 2015 at 08:16 UTC ( #1148483=perlmeditation: print w/replies, xml )  Need Help?? 
Hello Monks, This is something that has bugged some times to times : I have the feeling that the class hierarchy for numeric types in Perl 6 is upside down. Let me give you an example. The other day I wrote on rosetta code the following function to compute binomial coefficients:
I was quite happy about it, until I realized that the output was of type Rat, not Int. So I had to make an explicit conversion: sub infix:<choose> { ([*] ($^n ... 0) Z/ 1 .. $^p).Int }That was a bit annoying. Frankly, I expect something like 10/5 to be an integer, not a rational. I mean, I know it is a rational, but it also is an integer. Because normally in math, all integers are rationals. Their denominator is just 1. Things don't work like this in Perl 6. Numeric types are more about implementation than mathematics. Yet there is a feature in Perl 6 that could be used to make things work more like in math: subset Int of Rat where [%%] *.nude;If Int was defined as such, integers would be particular cases of rationals. In the same way, real numbers would be special cases of complex numbers: subset Real of Complex where { $_ == .conj };An other possibility would be: role Int does Rational { method nude { self, 1 }; ... }Or something like that, I don't know. Neither do I know if it would be possible or desirable to rewrite the whole Numeric hierarchy. Maybe it would not be worth the effort. But I do find it annoying that an Integer is not a Rat, or a Real not a Complex.
Back to
Meditations

