Since the check for variance positions of generic types hasn't been implemented yet, I'm having hard time to figure out the Kotlin code equivalent to following Scala code:
class Stock[+T](private val contents: List[T]) {
def add[U >: T](item: U) = new Stock[U](item :: contents)
def getAll(): Iterable[T] = contents
}
Is the following Kotlin code supposed to be allowed?
class Stock<out T> {
var contents = java.util.ArrayList<T>()
fun add<U>(item: U) where U : T {
contents.add(item)
}
}
So could I make a covariant mutable collection? If not, can I still use a constraint “where T : U” like in Scala to return a new immutable collection of type U?
The bug report (http://youtrack.jetbrains.com/issue/KT-252) doesn’t cover the usage of generic constraints.