This quesiton is based off of the answer in How to extend enums in Kotlin?.
I tried to implement one of the answers in this post, but it's not working as I expected it to. I want each implementation of this class to have a set of enums (the location of an Icon image) that is specific to each implementation. They all have the same implementation of several functions, just different strings and icons.
abstract class Platform(val name: String) {
abstract var icon: Icon
abstract sealed class Icon {
abstract val png: String
}
}
class Twitch : Platform("Twitch") {
override var icon: Icon = Icon.PURPLE
sealed class Icon {
data class PURPLE(val png: String = "drawable.twitch/glitch/01. Twitch Purple/glitch_flat_purple.png") : Icon()
}
}
This implementation currently gives me the error: Var-property type is models.Twitch.Icon, which is not a type of overridden public abstract var icon: models.Platform.Icon defined in models.Platform
Is there a way to actually accomplish this?
You try to put a Twitch.Icon
instance into a Platform.Icon
variable, two distinct and unrelated types. That does not work.
From what I gather you want PURPLE
to be part of the sealed hierarchy of Platform.Icon
. In that case you can simply remove the sealed class Icon
in Twitch
, that isn't needed.
There are some other minor errors, like the missing override
modifier for the png
property and the missing parantheses when you try to instantiate PURPLE
. But when all that is fixed the final result would look like this:
class Twitch : Platform("Twitch") {
override var icon: Icon = PURPLE()
data class PURPLE(override val png: String = "drawable.twitch/glitch/01. Twitch Purple/glitch_flat_purple.png") : Icon()
}