Is there any way to restrict possible type allowed...
# android
s
Is there any way to restrict possible type allowed? for example, i want to make a method that only accepts, Map<String, <String/Int/Bool>>, is there any way, i can restrict that? because using
Any
wont.
a
the functional way would be wrap it in a DU (Descriminated Union) type ( aka a sealed class) something like: Sealed Class TypesIWant{ data class StringWrapper(val value:String):TypesIWant ......... } and then a when wherever you need to unpack it
d
I believe what you're looking for is union types. This is not supported out of the box. AFAIK #arrow might implement it at some point, but not before a stable compiler plugin API is introduced.
s
Yeah, Alexs solution is a good hack for this but would have loved to have good out of box support for this.
a
yea that would be nice. Using a value class as the child may save a bit of overhead as opposed to the data class but, full disclosure I haven't tried that yet