Created
December 18, 2017 07:08
-
-
Save rustamgaifullin/db9f9895c68dcab94e38b646188e0dfb to your computer and use it in GitHub Desktop.
Example of when is construction in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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