When I first heard of value classes, I thought -- finally! Now I can define my own numeric types sans object allocations! But it turned out harder than I thought.
I want to define my own Decimal type, either Dec64
(http://dec64.com/) or long-backed decimal for fast monetary calculations. However, AnyVal
s can't extend Numeric
, as Numeric
is not a universal trait. I tried to follow up the Scala code for Double
, but it is quite complicated, with AnyValCompanion
, FractionalProxy
and lots of private[scala]
marked code, with very helpful comments like Should not be extended in user code.
So, how do I properly define my own numeric value type that can play well together with other Scala numbers?