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
Alex Prince
10/04/2021, 1:48 PM
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
dephinera
10/04/2021, 7:25 PM
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
shahroz
10/05/2021, 11:22 AM
Yeah, Alexs solution is a good hack for this but would have loved to have good out of box support for this.
a
Alex Prince
10/05/2021, 1:12 PM
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