You have an issue with your generic variance here.
The beginning of your code may look OK:
HijoA is-a ClaseA<HijoB, HijoC> by definition of the HijoA
ClaseA<HijoB, HijoC> is a subtype of ClaseA<out ClaseB, out ClaseC> by definition of the out variance
However, the problem is that there is nothing you can do with a ClaseA<out ClaseB, out ClaseC> as far as the method otherMethod is concerned.
Let me clarify:
out ClaseB means "some subtype of ClaseB"
out ClaseC means "some subtype of ClaseC"
- the method
otherMethod takes I and V as arguments, so in the case of ClaseA<out ClaseB, out ClaseC>, it takes "some subtype of ClaseB" and "some subtype of ClaseC" as arguments, but we don't know which ones (when seeing this type you don't know what the actual instance accepts exactly)
- any method can accept subtypes of its parameters (that's a general rule of inheritance)
Given the above, ClaseA<out ClaseB, out ClaseC> give us so little information that the only arguments that are guaranteed to be acceptable for the method otherMethod are:
- first argument: the subtypes of every possible subtype of
ClaseB
- second argument: the subtypes of every possible subtype of
ClaseC
Conclusion: the only generally acceptable type for both arguments is the type at the bottom of the hierarchy, which is the subtype of every type: Nothing. This essentially means you cannot call the method otherMethod in any meaningful way.
Solution
You have to be more specific on the element type of that list for these elements to by useful:
val list: MutableList<ClaseA<HijoB, HijoC>> = mutableListOf()
Or
val list: MutableList<HijoA> = mutableListOf()
Note that you ended up with this problem because you are trying to declare a variance that's the opposite of what the ClaseA's variance actually should be. It only takes I and V as inputs (method arguments), so you could declare your interface like this:
interface ClaseA<in I: ClaseB, in V: ClaseC> {
fun otherMethod(item: I, holder: V)
}
This would have highlighted early on that there was a problem with the declaration of list.
However, if you have other methods in ClaseA that return values of type I or V, then you won't be able to use this declaration-site variance. You can stick with your existing declaration of ClaseA then. But you still have to fix the declaration of list.