Hullaballoonatic
11/02/2019, 7:37 PMfun <N: Int | Byte> foo(n : N) = n.toDouble() / 4.5
TypeScript has them, so I'd imagine JB would want to have it for Kotlin/JS.nfrankel
11/02/2019, 7:41 PMInt
and Long
inherit from Number
and thus have the same functions available
but BigInteger
doesn’t
so it doesn’t make any senseHullaballoonatic
11/02/2019, 7:52 PMoperator fun BigInteger.plus(other: Int) = ...
fun <N: Int | BigInteger> foo(n: N) = n + 5
Number
subclasses you allow is to duplicate the function over and overnfrankel
11/02/2019, 8:04 PMInt
or BigInteger
on n
e.g. n.ushr(1)
cf. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/ushr.html
this is valid for Int
but not for BigInteger
do you see the issue?Mark Murphy
11/02/2019, 8:04 PMHullaballoonatic
11/02/2019, 8:05 PMToddobryan
11/02/2019, 8:07 PMBigInteger
is a subclass of Number
. As is BigDecimal
.Byte
, Short
, Int
, Long
, Float
, Double
, BigInteger
, and BigDecimal
(as well as AtomicInteger
, AtomicLong
, and Striped64
, if those are at all helpful) are all subtypes of Number
. There does seem to be some weirdness with type inference, though, which may have to do with the interplay of typealiases and the Java types--I don't know. I'm having to add an as Number
where it shouldn't be necessary to get things to compile.Hullaballoonatic
11/02/2019, 8:12 PMByte
, Short
, Int
, Char
, Long
, Float
, Double
all inherit from Kotlin's Number
, not Java's. BigInteger
, etc, being java libraries, would not inherit from Kotlin's numberToddobryan
11/02/2019, 8:16 PMNavigate->Type Hierarchy
on kotlin's Number
in IntelliJ, it shows you java.lang.Number
, suggesting it's a typealias.nfrankel
11/02/2019, 8:18 PMToddobryan
11/02/2019, 8:19 PMHullaballoonatic
11/02/2019, 8:19 PMnfrankel
11/02/2019, 8:21 PMn.ushr(1)
Toddobryan
11/02/2019, 8:23 PMushr()
isn't defined in Number
, then there's no problem.nfrankel
11/02/2019, 8:24 PMInt
, but not in BigNumber
so, how can you call it on n
?Hullaballoonatic
11/02/2019, 8:24 PMNumber
nfrankel
11/02/2019, 8:25 PMNumber
enough?Toddobryan
11/02/2019, 8:26 PMHullaballoonatic
11/02/2019, 8:26 PMToddobryan
11/02/2019, 8:27 PMarray
or string
. You need union types to handle those cases in a type-safe way.import java.lang.BigInteger
fun main(args: Array<String>) {
println(BigInteger("123") is Number)
}
kotlin.Number
and I get a highlight in IntelliJ saying "Check for instance is always true".Hullaballoonatic
11/02/2019, 8:31 PMmarstran
11/02/2019, 8:34 PMfun functionThatCanFail() : String | Failure {
// ...
}
when(val res = functionThatCanFail()) {
is Failure -> // Handle the error
is String -> // Do something with the result
}
Toddobryan
11/02/2019, 8:36 PMfold(onSuccess, onFailure)
pattern that we keep finding ourselves using.karelpeeters
11/02/2019, 9:25 PMjw
11/03/2019, 12:01 AMMark Murphy
11/03/2019, 11:48 AMstreetsofboston
11/03/2019, 2:22 PMraulraja
11/03/2019, 11:35 PM