Skip to content

Instantly share code, notes, and snippets.

@nbness2
Last active December 22, 2021 09:03
Show Gist options
  • Save nbness2/c08a25be27f99baf3ae6ba229cf36043 to your computer and use it in GitHub Desktop.
Save nbness2/c08a25be27f99baf3ae6ba229cf36043 to your computer and use it in GitHub Desktop.
Kotlin smartcasting example
interface Animal {
val name: String
val noise: String
fun makeNoise(): String = "$name made a noise: $noise"
}
fun useThing(thing: Any?) {
if (thing is Animal) {
println("thing is an animal!! what kind?? ${thing.name}")
println(thing.makeNoise())
} else if (thing is Int) {
println("thing is an Int! if you added 3 to it you would have: ${thing + 3}")
} else {
println("i don't really know what that is...")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment