Please can someone explain why this doesn’t compil...
# getting-started
m
Please can someone explain why this doesn’t compile:
Copy code
interface Sequenceable<out T> {
    fun asSequence(): Sequence<T>
}

interface Item

interface TypeOfItem: Item

interface Items: Sequenceable<Item>
    
interface TypeOfItems : Items, Sequenceable<TypeOfItem> // Error message: Type parameter T of 'Sequenceable' has inconsistent values: Item, TypeOfItem
https://pl.kotl.in/BSez9qCNJ
e
there's various reasons, but one that's easy to see: suppose there's a default implementation
Copy code
interface Items: Sequenceable<Item> {
    fun asSequence(): Sequence<Item> = sequenceOf()
}
now
TypeOfItems.asSequence()
, if it doesn't override
asSequence()
, does not return a
Sequence<TypeOfItem>
m
Ok, but what if there is not a default implementation?
e
the type checker does not know or care whether there is a default implementation or not. but even if the language did not allow for default implementations at all, you'd have to change the language definition to allow for this, and lose JVM compatibility
👍 2
m
I think the error message is a bit confusing. What would “consistent values” look like, other than “the same”?
e
fair point, Kotlin does require they be the same. but I think it might allow a raw type coming via Java? not sure, haven't tested
t
Can you make Items generic? The other issue that may be happening is implementing an interface twice.
Items<out T: Item> : Sequencable<T>
and then
interface TypeOfItems : Items<TypeOfItem>
If the naming
Items<Item>
at call site isn’t desirable you could typealias it
typealias PlainItems = Items<Item>