Why I keep getting this compile error: `Error:(2, ...
# announcements
h
Why I keep getting this compile error:
Error:(2, 44) Kotlin: Type parameter T is declared as 'in' but occurs in 'invariant' position in type SomeClass<String, T>
But it compiles when I remove the
in
keyword like this:
abstract class A<T>
? Given
someMethod(item: SomeClass<String, T>)
takes
T
as an input then using
in
should work in this case?
Copy code
abstract class A<in T> {
    fun someMethod(item: SomeClass<String, T>) {
        println(item)
    }
}

open class SomeClass<K,V>(
    override val entries: Set<Map.Entry<K, V>>,
    override val keys: Set<K>,
    override val size: Int,
    override val values: Collection<V>
) : Map<K,V>{
    override fun containsKey(key: K): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun containsValue(value: V): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun get(key: K): V? {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun isEmpty(): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}
k
I suggest you look up "invariance". There are quite a few useful resources about it.
r
@Foso I think you posted that to the wrong person @Hexa Because
V
is invariant in
SomeClass
. (Only
SomeClass
is in the
in
position, not its parameters.)
f
@Ruckus you are right, it was the wrong person
h
@Ruckus thnx