Question to the stdlib pros: why is there no exten...
# stdlib
o
Question to the stdlib pros: why is there no extension function
Number.toBigDecimal()
? I struggle with this a lot as there are specific functions
Int.toBigDecimal()
and so on, even for the real tricky cases like
Double.toBigDecimal()
which does the "right" thing using the String constructor of
BigDecimal
rather than the double constructor which may lead to real spooky results... So in my code, I write my own extension function:
Copy code
fun Number.toBigDecimal(): BigDecimal = when(this) {
    is Short -> this.toBigDecimal()
    is Int -> this.toBigDecimal()
    .......
}
This is highly repetitive and error prone... so, why is this function just missing from the standard lib?
y
Anyone can subclass
Number
and define their own type. There is no standard way to take an arbitrary user-defined
Number
class and find a
BigDecimal
that corresponds to it.
☝️ 3
o
Ok, I did not see Number as an open class anyone can use... yeah, makes sense. I just saw all of the
toDouble()
,
toInt()
functions defined inside
Number
and thought: where's
toBigDecimal()
- but I guess the answer to that simply is that
BigDecimal
is not a "platform class representing a number" as the KDoc states... I just use it so often cause it's on the JVM but maybe not on all platforms? If it were on all platforms, I would simply rephrase my question why this function is missing in the abstract Number class...
It's a bummer this cannot be something like a sealed hierarchy were we are certain about the number of Number types...
y
Adding
toBigDecimal()
as a required function in
kotlin.Number
would be conceivably possible (with a deprecation cycle and some warnings and such, and ofc a KMP
BigDecimal
type). The real issue is that
java.lang.Number
doesn't have such a method, and so there's java implementers of
Number
that we can do nothing about (note that
kotlin.Number
maps to
java.lang.Number
and vice versa)
👍 1
o
Didn't even think of Number coming from Java. So again, we suffer from Java's shortcomings. :( As BigDecimal literally is the only type that can cover all in terms of the contained value, it would be really sensible to have such a conversion function.
y
Number
is really badly designed anyways. It has no
plus
or other operators. It doesn't really have much sensible use. What would've been better is some interfaces like
Addable
,
Multipliable
, but that ship has sailed. With context parameters though, we can make something somewhat useful: https://github.com/kotlin-graphics/imgui/blob/master/core/src/main/kotlin/imgui/genericMath.kt. You could make something similar to convert numbers into
BigDecimal
easily, or, y'know, just stick with the extension method
k
BigDecimal literally is the only type that can cover all in terms of the contained value
Not really. There are, for example rational numbers (with several third-party implementations out there). A
BigDecimal
cannot represent
Rational(1, 3)
. On the other hand, rationals would be the only number type that could represent anything currently available in the stdlib (but not third-party libs).
👍 1