0

I have seen a lot of examples of interfaces for enums methods here but I am looking for another thing.

I want to assure some string enums have at least three keys there:

enum InterstitialEnum(val webName: string) {
    Showed("interstitialShowed"),
    Dismissed("interstitialDismissed"),
    Failed("interstitialFailed"),
    SomeInterstititalValue("intersititalSomeValue")
}

enum VideoEnum(val webName: string) {
    Showed("videoShowed"),
    Dismissed("videoDismissed"),
    Failed("videoFailed"),
    VideoSomethingHere("videoSomethingHere")
}

My end goal is to use that interface as function parameter, so I can access functionParameter.Showed.webName, etc.

I tried to create an interface but I can not find a way to define Showed, Dismissed or Failed, just functions.

This does not work

interface BaseEnum {
    val FailedToShow: String;
}

Edit:

Important, this is not a duplicate of How to extend enums in Kotlin? because I do not want the same key/value pair, I want the same key with different value.

distante
  • 6,438
  • 6
  • 48
  • 90
  • Does this answer your question? [How to extend enums in Kotlin?](https://stackoverflow.com/questions/56749752/how-to-extend-enums-in-kotlin) – duffymo Apr 25 '21 at 11:48
  • No because I do not want the same `key`/`value` pair, I want the same `key` with different `value` – distante Apr 25 '21 at 11:56

2 Answers2

2

You can’t do this with different enums because there’s no mechanism for relating the names of enum instances of different enums.

Here’s an idea for something that’s similar to the structure you’re looking for.

interface WebNames {
    val showed: String
    val dismissed: String
    val failed: String
}

object InterstitialWebNames: WebNames {
    override val showed: String = "interstitialShowed"
    override val dismissed: String = "interstitialDismissed"
    override val failed: String = "interstitialFailed"
}

object VideoWebNames: WebNames {
    override val showed: String = "videoShowed"
    override val dismissed: String = "videoDismissed"
    override val failed: String = "videoFailed"
}

If you have other properties, you could use a wrapper class instead of Strings for these properties.

C.Schone
  • 71
  • 1
  • 2
  • 9
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
1

I think @TenFour04's example is the closest you're going to get. You can think of an enum as a type, and Showed, Dismissed and Failed as subtypes - but there's no way of enforcing that a particular supertype must have a certain set of subtypes, with specific names.

If you don't just want to deal with String properties (e.g. so you can do something like if (state is Showed) then you could make a type for that:

open class State(val webName: String)
class Showed(webName: String) : State(webName)
class Dismissed(webName: String) : State(webName)
class Failed(webName: String) : State(webName)

interface WebNames {
    val showed: Showed
    val dismissed: Dismissed
    val failed: Failed
}

object VideoWebNames : WebNames {
    override val showed = Showed("videoShowed")
    override val dismissed = Dismissed("videoDismissed")
    override val failed = Failed("videoDismissed")
    // a State that's not a standard one included in the interface
    val videoSomethingHere = State("videoSomethingHere")
}

if you wanted you could stick all the required states in a sealed class, to group them together and maybe do some checking later

open class State(val webName: String)

sealed class RequiredState(webName: String) : State(webName)
class Showed(webName: String) : RequiredState(webName)
class Dismissed(webName: String) : RequiredState(webName)
class Failed(webName: String) : RequiredState(webName)

So now videoWebNames.showed is a State that also is Showed and is RequiredState

cactustictacs
  • 17,935
  • 2
  • 14
  • 25