I’m writing some code that involves iterating over heterogeneous collections, and I’m wondering if there’s a way to write a function that encodes an internal “is” check. I’m used to TypeScript, which has a lot more of this kind of thing.
For example,
data class Foo(val color: String)
// This syntax borrowed from TypeScript
// This works if I return Boolean, but doesn't help type narrowing in other contexts
fun isRedFoo(foo: Any?): foo is Foo {
return foo is Foo && foo.color == "red"
}
fun doSomethingToFoo(foo: Foo) { ... }
val items = listOf<Any>(...)
items.forEach {
when {
// If I use Boolean for the return type of isRedFoo, Kotlin yells at me because
// it doesn't know that "it" is of type Foo
isRedFoo(it) -> doSomethingToFoo(it)
else -> println("skipping $it")
}
}
I know I can write (it is Foo && isRedFoo(it)) -> ...
but that’s deeply less satisfying (and that’s the only criterion that really matters ). It also doesn’t work if the type that doSomethingToFoo
expects changes (I’d kind of like to decouple that from this part of the code).