Sangeet
07/06/2022, 3:30 PMfun <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.Chris Lee
07/06/2022, 3:43 PMSomething(doSomething<String>())
), but would expect an implicit, unchecked cast to be flagged.Sangeet
07/06/2022, 3:48 PMval value: Int = doSomething()
shows error. But passing that to a parameter doesn't show an error.Sam
07/06/2022, 3:49 PMChris Lee
07/06/2022, 3:52 PMval y = doSomething()
, with not enough information to infer type. Yet the same method call as a parameter compiles…Chris Lee
07/06/2022, 4:02 PMephemient
07/06/2022, 6:12 PMT = 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