Replies: 2 comments
-
I think you're correct that this is a problem, but I don't think use Math::BigInt;
my $x = Math::BigInt->new(12);
my $y = Math::BigInt->new(5);
say $x / $y;
say $x + $y; Those are overloaded objects and they do the right thing. By your suggestion, this would break: use Math::BigInt;
my $x :of(INT) = Math::BigInt->new(12);
my $y :of(INT) = Math::BigInt->new(5);
say $x / $y;
say $x + $y; That's not going to match people's expectations. If the overload is broken, fix the class. I think |
Beta Was this translation helpful? Give feedback.
-
In some cases you might want such things to throw an exception / be invalid operations. For example, this is what statistical tools do with https://en.wikipedia.org/wiki/Level_of_measurement. That's what you'd want when dealing with certain units of measure, dates, etc. More here: https://pubmed.ncbi.nlm.nih.gov/24987515/. |
Beta Was this translation helpful? Give feedback.
-
It is reasonable for someone who has declared:
To assume that
my $y = $x * 1
will then be safe code, and they don't need to wrap it intry
, or addno warnings "numeric"
.However, as things currently are, it can throw an exception.
Why?
$x
could be an object which overloads0+
but doesn't overload*
.https://toby.ink/blog/2023/04/02/experiments-in-overloading/
Accepting overloaded objects by default is dangerous. However, rejecting them by default pushes overloaded objects further into the realms of being "second-class citizens". I don't have a solid solution for this, but it's something to consider.
(A possible solution might be for Perl itself to always enable numeric fallbacks when
0+
is overloaded, always enable string fallbacks when""
is overloaded, and always enable logic fallbacks whenbool
is overloaded, regardless of whether the class declaresfallback => 1
.)Beta Was this translation helpful? Give feedback.
All reactions