Kamil K
09/30/2019, 10:34 AMfun x(anInt: Int) : String {
when (anInt) {
0 -> return "zero"
1 -> return "one"
}
return null!!
}
Despite the 'quality` of code itself I'm curious why compiler thinks that return null!!
is fine (IDE hints that return statement is unreachable which is false)?
And similarly why this val a : Int = null!!
is allowed as well.gian
09/30/2019, 10:37 AMthrow Exception()
marstran
09/30/2019, 10:57 AMnull!!
has type Nothing
, which is a subclass of every type. You can therefore return it from everything (for the same reason you can throw exceptions). The return
is unreachable because there is an expression of type Nothing
before it.Kamil K
09/30/2019, 12:17 PMkarelpeeters
09/30/2019, 8:00 PMnull
is `Nothing?`:marstran
09/30/2019, 8:05 PMnull!!
becomes Nothing
.Paul Woitaschek
10/01/2019, 6:32 AMelse -> error("Invalid anInt=$anInt")