Any idea why lint or kotlin compiler is not showin...
# getting-started
s
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
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
Even
val value: Int = doSomething()
shows error. But passing that to a parameter doesn't show an error.
s
Interesting... it is a compile error under 1.3. But not 1.4+.
c
…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…
e
indeed,
T = CharSequence & Int
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
T =
other subtypes of
CharSequence
, for example
StringBuilder
or
javax.lang.model.element.Name