rocketraman
04/17/2020, 7:58 PMfun <T> foo(): T? = "abc" as T
fun main() {
val s: String? = if(false) null else foo()
}
1️⃣ A runtime exception ClassCastException: class java.lang.String cannot be cast to java.lang.Void
2️⃣ A compile-time error due to the type T
of foo
being ambiguous (could be String?
or could be Nothing
)
3️⃣ A compile-time warning that the type of T
was inferred to Nothing
and that is probably not the desired result
4️⃣ An inference of T
to be String?
, so no compile-time or runtime errors
5️⃣ Otherrocketraman
04/17/2020, 8:06 PMfun <T> bar(): T = "abc" as T
fun main() {
val s: String? = if(false) null else bar()
}
produces a runtime exception 1️⃣, but at least it comes with a compile time warning 3️⃣.mikhail.zarechenskiy
04/23/2020, 10:54 AM