Skip to content

Instantly share code, notes, and snippets.

@zeroarst
Last active July 10, 2020 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeroarst/288dfecb7523ad126432665f59509839 to your computer and use it in GitHub Desktop.
Save zeroarst/288dfecb7523ad126432665f59509839 to your computer and use it in GitHub Desktop.
Chaining Kotlin Type Casting and following execution together!! (1)
// add this !!
infix fun <T, R> T?.then(block: T.() -> R): R? = this?.block()
abstract class Animal
class Cat : Animal() {
fun say() = "meow"
}
fun main() {
val myCat: Animal = Cat()
myCat as? Cat then { println(say()) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment