Mattias Flodin
08/06/2020, 5:37 PMinterface Source<out T> {
fun nextT(): T
}
fun demo(strs: Source<String>) {
val objects: Source<Any> = strs // This is OK, since T is an out-parameter
// ...
}
If I replace String by a type argument T, it no longer compiles and says "Type mismatch: inferred type is Source<T> but Source<Any> was expected". Why is that? I had expected it work just like in the example.nanodeath
08/06/2020, 5:40 PMT
Mattias Flodin
08/06/2020, 5:42 PMinterface Source<out T> {
fun nextT(): T
}
fun <T> demo(strs: Source<T>) {
val objects: Source<Any> = strs // Type mismatch: inferred type is Source<T> but Source<Any> was expected
}
As far as I can see there is only one T in demo(), and so it doesn't need to be "the same" as anything else?Mattias Flodin
08/06/2020, 5:43 PMaraqnid
08/06/2020, 6:16 PMAny
isn’t the ultimate supertype, Any?
isaraqnid
08/06/2020, 6:16 PMT
is non-nullable, you should specify <out T : Any>
araqnid
08/06/2020, 6:18 PMString
is a subtype of Any
, so it’s consistent that that would workaraqnid
08/06/2020, 6:26 PMSource<Any?>
if that’s what you meantMattias Flodin
08/06/2020, 6:31 PMthanksforallthefish
08/07/2020, 6:36 AMAny?
at least once a week and still think Any
is the ultimate supertype when writing, just to have the compiler reminding me I once again forgot about nullsMattias Flodin
08/07/2020, 6:56 AMthanksforallthefish
08/07/2020, 9:00 AM