Another generics-related question :slightly_smilin...
# getting-started
n
Another generics-related question 🙂 Is there a was to make this compile or should I somehow refactor it? Thanks.
Copy code
interface C<B>

interface C1<B> : C<B>

interface C2<B, F : B> : C<B>

interface D<B, F : B, T : B> {
    val f: (C<B>) -> T
}

interface D1<B, T : B> : D<B, Nothing, T> {
    override val f: (C1<B>) -> T // Compile error: not a subtype type of overridden ...
}

interface D2<B, F : B, T : B> : D<B, F, T> {
    override val f: (C2<B, F>) -> T // Compile error: not a subtype type of overridden ...
}
r
This is not actually generic related at all With a bit of naming to get things clearer, and wheezing away with the generics, we get the same thing, with the same error:
Copy code
interface SuperType
interface SubType1: SuperType
interface SubType2: SuperType 

interface Contract {
    val f: (SuperType) -> Unit
}

interface SubContract1 : Contract {
    override val f: (SubType1) -> Unit // Compile error: not a subtype type of overridden ...
}

interface SubContract2 : Contract {
    override val f: (SubType2) -> Unit // Compile error: not a subtype type of overridden ...
}
Why? It's because it's a function argument. Consider this: we wouldn't get an error here, and we would have succefully implemented
SubContract1Impl
somehow. Now this code:
Copy code
// it's the implementation, so it's subtype of it
val subContract1: SubContract1 = SubContract1Impl()
// SubContract1 extends Contract
val contract: Contract = subContract1

// SubType2 is subtype of SuperType, so this passes
// but whoever implemented f, only expected SubType1...
contract.f(SubType2Impl())
🙏 1
1
this concept is called contravariance eg, a
(A1) -> B1
is subtype of
(A2) -> B2
only when
B1
is subtype of
B2
, and
A2
is subtype of
A1
Notice the order.
🙏 1
eg, a function need to expect at least as general arguments as the interface it implements it can expect either more general arguments, or return a narrower type
n
Thanks, your explanations are very easy to follow - you should write a book 😉