Any idea why lint or kotlin compiler is not showing error for below code
Copy code
fun <T : CharSequence> doSomething(): T {
return String() as T
}
class Something(intValue: Int)
Something(doSomething()) // Doesn't show any compile error
It seems that it is automatically casting CharSequence to Number and throwing error in runtime.
c
Chris Lee
07/06/2022, 3:43 PM
Hmm. The generic type is missing on the method invocation (this produces a compilation error
Something(doSomething<String>())
), but would expect an implicit, unchecked cast to be flagged.
s
Sangeet
07/06/2022, 3:48 PM
Even
val value: Int = doSomething()
shows error. But passing that to a parameter doesn't show an error.
s
Sam
07/06/2022, 3:49 PM
Interesting... it is a compile error under 1.3. But not 1.4+.
c
Chris Lee
07/06/2022, 3:52 PM
…and this is a compile error:
val y = doSomething()
, with not enough information to infer type. Yet the same method call as a parameter compiles…
here, and while the are no instances of that type at runtime, that has no effect on type checking.
Note that your original code is broken in the same way for