Hi guys. How can I override a variable of a derive...
# announcements
c
Hi guys. How can I override a variable of a derived interface type. I have the interfaces:
Copy code
interface ExperimentManagerDelegateCore {

interface ExperimentManagerDelegate:ExperimentManagerDelegateCore {
I have the classes:
Copy code
abstract class ExperimentManagerCore {

    open var delegate: WeakReference<out ExperimentManagerDelegateCore>? = null
}

class ExperimentManager():ExperimentManagerCore() {

    override var delegate: WeakReference<ExperimentManagerDelegate>? = null
}
but in
ExperimentManager
I got the error:
Copy code
Type of 'delegate' doesn't match the type of the overridden var-property 'public open var delegate: WeakReference? defined in ExperimentManager.ExperimentManagerCore'
I thought I would fix it with
out ExperimentManagerDelegateCore
Any ideas?
m
I think it's because
delegate
is a
var
. The setter causes the out-type to appear in an in-position (the setter's parameter).
At least it compiles if you make it a
val
You could actually make it a
val
in
ExperimentManagerCore
, and then override it with a
var
in
ExperimentManager
😛
c
I don’t think that would work for me. But I tried and I don’t make it work with a val. I get the same error and also I get other errors too because it doesn’t suppose to be a val and I try to change it
But thanks for trying 🙂
m
Actually, another thing you could try is to make
ExperimentManagerCore
generic. Like this:
Copy code
abstract class ExperimentManagerCore<T :  ExperimentManagerDelegateCore> {
    open var delegate: WeakReference<T>? = null
}

class ExperimentManager() : ExperimentManagerCore<ExperimentManagerDelegate>() {
    override var delegate: WeakReference<ExperimentManagerDelegate>? = null
}
c
Yes, I think that’s the answer, I was changing everything and it seems to compile. Thanks.
Very clever 🙂