https://kotlinlang.org logo
Title
h

Hullaballoonatic

11/29/2018, 9:59 PM
is there a unifying comparable class to which all primitive numbers belong, or better yet, one on which you can use mathematical operators?
s

Shawn

11/29/2018, 9:59 PM
I think
Number
is the closest thing to what you’re asking for
It might not necessarily define operators, though
h

Hullaballoonatic

11/29/2018, 10:00 PM
unfortunately i can't compare any primitive number types to Number... tried
s

Shawn

11/29/2018, 10:01 PM
it looks like
Int
, for example, extends
Number
and implements
Comparable<Int>
, so I don’t think there’s another intermediate type that does the same
h

Hullaballoonatic

11/29/2018, 10:01 PM
I'm making a
RestrictedNumber
delegate(s), starting with
RestrictedInt
, and it'd be nice to not have to make a
compareTo()
for every number type
yep, that's what i saw myself, and just wondered if I was missing something
k

karelpeeters

11/29/2018, 10:38 PM
I'm going to keep saying this but you can't do anything with a Number! You can't write code to compare them, you can't do write code to do math with them.
h

Hullaballoonatic

11/29/2018, 10:38 PM
it's such a shame ;_;
but it's not a big deal
k

karelpeeters

11/29/2018, 10:39 PM
Well it's a fundamental problem, they could't have "fixed" that.
h

Hullaballoonatic

11/29/2018, 10:39 PM
I'm intrigued. What makes it fundamental?
because the bit twiddling doesn't work out? you'd always have to convert one?
k

karelpeeters

11/29/2018, 10:41 PM
Let's say I have a
Fraction: Number
and a
BigInteger: Number
, and I add them. Now what? Neither fits into the other. In general there might be tens of Number implementations that then all need to be math-compatible with each other.
h

Hullaballoonatic

11/29/2018, 10:42 PM
so why can't we add ints to longs, or floats to doubles?
or booleans to booleans?
scratch the last one... that's dumb
k

karelpeeters

11/29/2018, 10:43 PM
I'm not even talking about the bit fiddling side or whatever, just the polymorphism part.
h

Hullaballoonatic

11/29/2018, 10:43 PM
ah
i just always found it strange that kotlin didn't include the autocasting to larger types like int -> long, int -> double, etc
because you don't lose or corrupt any data doing that
k

karelpeeters

11/29/2018, 10:45 PM
Yeah that's kind of annoying, I agree with you there. When I say that this is a fundamental problem I mean there's nothing you can write in a function like
fun add(x: Number, y: Number): Number
that makes it work out.
h

Hullaballoonatic

11/29/2018, 10:48 PM
yeah, and therefore you can't either create your own number types easily either. you can do it, it's just tedious to make universally operable and comparable
k

karelpeeters

11/29/2018, 10:48 PM
(it's impossible, there's no way to know all other implementations in advance)
h

Hullaballoonatic

11/29/2018, 10:49 PM
what's impossible? making a generic comparable and operable number type?
k

karelpeeters

11/29/2018, 10:49 PM
Yes.
h

Hullaballoonatic

11/29/2018, 10:49 PM
yeah, it seems that way