rednifre
11/21/2024, 9:59 AMNumber
super type in Kotlin, which is the super type of numbers like Int
Float
etc. Should my custom number type (a little Decimal type object that wraps a Long and has some custom rounding rules) also inherit from Number
? What are the pros and cons? Would there be any gnarly surprises later?
If I remember correctly, Java does not have a Number
super type, so what were the reasons for adding it to Kotlin? What does it do for Kotlin?dmitriy.novozhilov
11/21/2024, 10:00 AMInteger
, Long
, Double
and other number types
That's the main reason to have it in Kotlinrednifre
11/21/2024, 10:01 AMdmitriy.novozhilov
11/21/2024, 10:05 AMNumber
is an arbitrary abstract class, you can extend it
> Would it even make any practical difference if my number type extended from Number?
Well, it will allow you to use your numbers in some abstract APIs which can work with java.lang.Number
. Do you need it or not depends on which libraries/frameworks you useDaniel Pitts
11/21/2024, 4:11 PMNumber
class exposes (which I believe is just toInt, toLong, etc...), you can decide whether or not it is useful for your class to extend it. If you find that you're passing instances of your class to a lot of places that take Number
, and you end up converting it there anyway, then it might make sense to just extend Number
. If you don't, then maybe not.Klitos Kyriacou
11/21/2024, 4:35 PM@JvmInline value class
if you haven't already.rednifre
11/21/2024, 4:36 PMrednifre
11/21/2024, 4:44 PM