Consider the following Kotlin interface:
interface Box<out T : SomeType> {
val item: T
}
Implementing this with Kotlin would look something like this:
data class BoxImpl<out T : SomeType>(override val item: T) : Box<T>
Now I want to convert that interface to Java:
public interface Box<T extends SomeType> {
T getItem();
}
But implementing this in Kotlin causes a problem:
data class BoxImpl<out T : SomeType>(private val item: T) : Box<T> {
override fun getItem(): T {
return item
}
}
The problem is that the Kotlin compiler complains that T
is invariant on the Java interface, but covariant on the implementation.
So, how do you specify out
(covariant) variance in Java?
Additionally, how would you specify in
(contra-variant) variance?