When working with strings you can check if a string belongs to a string set like this:
if (a in listOf("ab", "abab")) {
// do something
}
Or:
when (a) {
in listOf("akshd", "sd") -> {
// do something
}
}
Now, let's imagine you have a sealed class:
sealed class SealedClass {
class Subclass1 : SealedClass()
class Subclass2 : SealedClass()
class Subclass3 : SealedClass()
}
There's a nice way to check if some object b is in the subset of these types. If you use when expression you can write something like this:
when (b) {
is SealedClass.Subclass1, is SealedClass.Subclass2 -> {
// do something
}
}
Things are getting complicated if you want to use if expression. You can write something like this:
if (b is SealedClass.Subclass1 || b is SealedClass.Subclass2) {
// do something
}
Or this:
if (
b::class in listOf(
SealedClass.Subclass1::class,
SealedClass.Subclass2::class
)
) {
// do something
}
Both solutions above are quite clumsy.
Is there a more elegant way to express such a check when using the if statement?