Skip to content

Instantly share code, notes, and snippets.

@rustamgaifullin
Created December 18, 2017 07:08
Show Gist options
  • Save rustamgaifullin/db9f9895c68dcab94e38b646188e0dfb to your computer and use it in GitHub Desktop.
Save rustamgaifullin/db9f9895c68dcab94e38b646188e0dfb to your computer and use it in GitHub Desktop.
Example of when is construction in Kotlin
import java.io.IOException
import java.net.UnknownHostException
open class Foo {}
class Bar: Foo() {}
fun getFooOrBar(fooOrBar: Foo): String {
return when (fooOrBar) {
is Foo -> "Foo"
is Bar -> "Bar"
else -> "Unknown"
}
}
fun getMessageForException(exception: Throwable): String {
return when (exception) {
is IOException -> "IOException"
is UnknownHostException -> "UnknownHostException"
else -> "Exception"
}
}
fun main(args: Array<String>) {
println(getFooOrBar(Bar()))
println(getMessageForException(UnknownHostException()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment