holgerbrandl
01/09/2022, 7:40 PMNumber.minOrNull()
?Dominaezzz
01/09/2022, 7:57 PMholgerbrandl
01/09/2022, 7:58 PMDominaezzz
01/09/2022, 7:58 PMfun Number.minOrNull(): Number
holgerbrandl
01/09/2022, 7:59 PMList<Number>.minOrNull()
holgerbrandl
01/09/2022, 8:00 PMNumber
is not comparable in kotlin. Why would that be?Dominaezzz
01/09/2022, 8:03 PMDominaezzz
01/09/2022, 8:06 PMholgerbrandl
01/09/2022, 8:07 PMDominaezzz
01/09/2022, 8:07 PMDominaezzz
01/09/2022, 8:07 PMholgerbrandl
01/09/2022, 8:08 PMDominaezzz
01/09/2022, 8:09 PMComparableNumber
.Dominaezzz
01/09/2022, 8:10 PMholgerbrandl
01/09/2022, 8:10 PMRealNumber
Dominaezzz
01/09/2022, 8:10 PMT : Number, Comparable<T>
I guess.Dominaezzz
01/09/2022, 8:12 PMholgerbrandl
01/09/2022, 8:13 PMList<Number>
and want to know its minimum. I know I can map them, but I was just curious why I have to.Dominaezzz
01/09/2022, 8:44 PMList<Number>
, why not a more specific type?Dominaezzz
01/09/2022, 8:44 PMNumber
used in practice.holgerbrandl
01/09/2022, 8:46 PMholgerbrandl
01/09/2022, 8:47 PMephemient
01/09/2022, 11:04 PMephemient
01/09/2022, 11:18 PMephemient
01/09/2022, 11:20 PMephemient
01/09/2022, 11:22 PMholgerbrandl
01/11/2022, 11:03 AMNiko
01/23/2022, 11:47 AMNumber
is mostly about having common OO interface for transforming numbers between different representations, but it doesn't want to force a possibly unwanted way of comparing the values, like our brain does it:
Comparing 1
and 2
(ints) is easy, but for 1
and 1.1
(int, float) our brain automatically goes into floating points mode and is able to do the comparison, but for the computer system, it first needs to convert one or the other into a binary pattern (where integers and floating points have completely different layout) to be able to do a comparison.
I'm assuming the Java team never wanted to force an implicit, possibly resource intensive conversion for comparisons, thus leaving it out.
I have some maths libraries developed, where every outside-facing API method receiving data has a signature with Number
type, but internally everything is IntArray
or DoubleArray
base on the wrapper's type and converted to/from at the boundaries, thus offering the stats operations with an explicit possibility to use the "least precise" type if double precision isn't neededephemient
01/23/2022, 11:49 AMephemient
01/23/2022, 11:51 AMNiko
01/23/2022, 11:51 AMephemient
01/23/2022, 11:54 AMdata class Int128(val high: Long, val low: Long) : Number() { ... }
and unless your code knows about the type, there's not really anything it can do with itephemient
01/23/2022, 11:55 AMephemient
01/23/2022, 11:57 AMNumber
to be a code smell. it isn't possible to express which subtypes of number the code can actually work with, and there's no way to generically handle all types of numbersNiko
01/23/2022, 12:02 PMNumber
type (in Java and Kotlin) are a bit weird if they are viewed as if it represented a number like in real world.
When it comes to (my) libraries supporting custom number types, the general contract holds: any Number
subtype must implement toInt, toDouble
etc and my library will handle its value accordingly, given which ever wrapper type context is chosen by the userephemient
01/23/2022, 12:18 PMNumber
but will always have lossy `toInt`/`toDouble`? at that point I would just wire up overloads for all the primitive numeric types, since that's all you're going to be able to work with.Niko
01/23/2022, 12:27 PMT : Number
will rid the user of doing an additional to*
method calls as I'm able to do it for them, to fit the documented, internal representation of the data.
I not sure how any of this contradicts on the issue we both agree upon: Number
doesn't implement Comparable
to other number subtypes, as it wouldn't make sense in common terms, without the developers' input about the context in which the comparison would be doneephemient
01/23/2022, 12:39 PMf(Byte) f(UByte) f(Short) f(UShort) f(Int) f(UInt) f(Long) f(ULong) f(Float) f(Double)
then the user is similarly able to use your f
regardless of which numeric type they have (and in fact they're in an even better position, because UByte UShort UInt ULong
do not extend Number
), while also declaring, at the type level, that BigInteger
needs to be converted.holgerbrandl
01/23/2022, 1:06 PMephemient
01/23/2022, 11:25 PM