https://kotlinlang.org logo
c

Cicero

11/26/2020, 12:57 PM
I had this
Copy code
abstract class AbstractResourceTopic(val state: ResourceState? = null, val isOn: Boolean? = null) {
    abstract val resourceType: ResourceType
}
and this
Copy code
fun AbstractResourceTopic.getIcon(): Int {
    this.state?.let {
        return when (it.state) {
            0 -> getStateZeroIcon()
            1 -> getStateOneIcon()
            2 -> getStateTwoIcon()
            else  -> getStateZeroIcon()
        }
    }
    this.isOn?.let {
        return if (isOn) getOnIcon() else getOffIcon()
    }
    return getStateZeroIcon()
}
I turned into this
Copy code
abstract class AbstractResourceTopic {
    constructor(state: ResourceState)
    constructor(isOn: Boolean)
    abstract val resourceType: ResourceType
}
and I don’t know how to create extension classes for this two different constructors
Copy code
fun AbstractResourceTopic.getStateIcon(): Int {
        return when (it.state) {
            0 -> getStateZeroIcon()
            1 -> getStateOneIcon()
            2 -> getStateTwoIcon()
            else  -> getStateZeroIcon()
        }
    }
}
fun AbstractResourceTopic.getIsOnIcon(): Int {
    return if (isOn) getOnIcon() else getO
}
This is not working and I couldn’t find the correct syntax or if I can even do this
r

ribesg

11/26/2020, 1:27 PM
Your constructor parameters aren't properties
👌🏽 1
n

nrobi

11/26/2020, 1:28 PM
Untitled
your
AbstractResourceTopic
doesn’t have neither a
state
nor an
isOn
field this way
c

Cicero

11/26/2020, 1:31 PM
Aham
Thank you so much