Let's say I have an expensive function called doHardThings() which may return various different types, and I would like to take action based on the returned type. In Scala, this is a common use of the match construct:
def hardThings() = doHardThings() match {
case a: OneResult => // Do stuff with a
case b: OtherResult => // Do stuff with b
}
I'm struggling to figure out how to do this cleanly in Kotlin without introducing a temporary variable for doHardThings():
fun hardThings() = when(doHardThings()) {
is OneResult -> // Do stuff... with what?
is OtherResult -> // Etc...
}
What is an idiomatic Kotlin pattern for this common use case?