Problem with Returning Collection<KProperty1<AnyClass extends BaseType, *>

Hello guys
I’m trying to do it where I have an interface, say Base. I expect the implementors to override a function and return the list of their own member properties.

Let’s say the base interface is

interface Base {
fun getMemberProperties(): Collection<KProperty1<Base, *>>
}

Let’s say there’s a class Implementor that implements this Base interface.

data class Implementor: Base {
override fun getMemberProperties(): Collection<KProperty1<Implementor, *>>
}

The problem is the return type of the implementing class is Collection<KProperty1<Implementor, *>> as compared to Collection<KProperty1<Base, *>> and it doesn’t compile.

So how can I get the function signature in the interface to say any class of the Base type and not exactly Base type.

So this is similar to Java’s ? extends Base and I’ve read up on Kotlin’s covariance but it’s not helped me.

Help appreciated. Currently I’m getting away by returning Collection<KProperty1<,>>

Help appreciated.

I believe what you are looking for is this.

interface Base {
    fun getMemberProperties(): Collection<KProperty1<out Base, *>>
}

When used like this, the out keyword basically means the same as ? extends Base in Java.

As an aside, consider whether it would make more sense to define memberProperties as a read-only property, possibly without a backing field in the implementations.

Thank you. That was the bit of syntax I needed to figure out.