ccd
ccd

Reputation: 6948

Kotlin mix type

How to limit the type instead of using Any in value of map?

val mixType = Map<String, String or Boolean or Int>()  // something I like
val anyType = Map<String, Any>()  // the scope is too large

Upvotes: 3

Views: 225

Answers (1)

Konstantin Raspopov
Konstantin Raspopov

Reputation: 1625

I would suggest using a sealed class here:

sealed class MyValue {
    class IntValue(val value: Int): MyValue()
    class BooleanValue(val value: String): MyValue()
    class StringValue(val value: Boolean): MyValue()
}

Upvotes: 2

Related Questions