Similar question to the above, but with covariance...
# announcements
m
Similar question to the above, but with covariance this time. If I undestand correctly: *
T
is String *
out T
means that String or any of its subtypes can fit this * Since
String
in
methodB
matches that, why I can't pass
methodB
into
methodA
?
p
i think you’re declaring the variance in the wrong place. you’re saying
methodA
needs a container
out String
as its parameter, and you’re passing in a container
String
, which isn’t the same. if the container data class was defined as
out T
, and methods A and B just specified as T, I think it would be different. in both the cases. i don’t have the time right now to work it all out, but that would be my guess 🙂
t
Because, by type signature of methodB, it may perform invariant operation on input. Let's say Container would have method set(value : T) - functionB is allowed to call it, but it's not allowed to call it on Container<out *>
To be more illustrative:
Copy code
data class Container<T>(var value: T)

fun methodB(input: Container<String>) {
	input.value = "B"
}
m
aaaah I see
thanks a lot!