In Kotlin, the Number
type sounds quite useful: A type to use whenever I need something numeric.
When actually using it, however, I quickly noticed it is pretty useless: I cannot use any operators on these numbers. As soon as I need to do something with them, I need to explicitly convert them (even for comparing).
Why did the language designers choose to not include operators in the Number
specification?
Thinking on this, I noticed it could be tricky to implement Number.plus(n: Number): Number
, because n
might be of a different type than this
.
On the other hand, such implementations do exist in all Number
subtypes I checked. And of course they are necessary if I want to type 1 + 1.2
, which calls Int.plus(d: Double): Double
The result for me is that I have to call .toDouble()
every time I use a number. This makes the code hard to read (compare a.toDouble() < b.toDouble()
with a < b
).
Is there any technical reason why operators where omitted from Number
?