Hullaballoonatic
06/14/2019, 12:24 AMNumber
not operable in the JVM? Why did Sun design it that way and why does JB not fix it in Kotlin? Everything (afaik) that inherits from Number
is operable, so why wouldn't the operators be refactored to be included there?Shawn
06/14/2019, 1:37 AM4 + NaN
equal?Hullaballoonatic
06/14/2019, 3:09 AMShawn
06/14/2019, 3:10 AMHullaballoonatic
06/14/2019, 3:10 AMerror("You cannot add NaN to a number")
Shawn
06/14/2019, 3:12 AM4.0
, NaN
, and 6.9
are all valid float values - you can’t vary the return type of a function based on the value of its argumentsHullaballoonatic
06/14/2019, 3:12 AMNumber
class has the following added:
abstract operator fun <N: Number> plus(other: N): N
// etc for others
Shawn
06/14/2019, 3:14 AMHullaballoonatic
06/14/2019, 3:15 AMNumber
become
abstract class Number<N: Number<N>>()
and therefore you can only do the operations on a number of the same typeShawn
06/14/2019, 3:16 AMHullaballoonatic
06/14/2019, 3:16 AMShawn
06/14/2019, 3:18 AMHullaballoonatic
06/14/2019, 3:23 AMShawn
06/14/2019, 3:24 AMHullaballoonatic
06/14/2019, 3:24 AMfun adder<N: Number>(a: N, b: N): N = a + b
Shawn
06/14/2019, 3:26 AMNumber
(int) 4
Hullaballoonatic
06/14/2019, 3:27 AMShawn
06/14/2019, 3:28 AMfloat
up to a BigDecimal
?Number
throughout, that seems like a nightmare to debugadder()
function as is - just with a lot of runtime typecheckingreturn when (lhs) {
is Double -> when (rhs) {
is Double -> lhs + rhs // return a Double, but it's a Number™