The following code tells everything:
class A<E>(val a: E) {
fun <T> get(s: T?) = a as T
fun get(): E = get(null) //Compiles OK, but throw NullPointerException when running.
fun get2(): E = get<E>(null) //Compiles OK and no exceptions.
}
fun main(args: Array<String>) {
val a = A(1)
println(a.get2())
println(a.get())
}
running result is
1
Exception in thread "main" java.lang.NullPointerException
at A.get(Bug.kt:13)
at A.main(Bug.kt:21)