summaryrefslogtreecommitdiffhomepage
path: root/kotlin.html.markdown
diff options
context:
space:
mode:
authorSergey Mashkov <cy6erGn0m@gmail.com>2016-10-05 14:34:08 +0300
committerven <vendethiel@hotmail.fr>2016-10-05 13:34:08 +0200
commit98bb8f8432e66c5721bb7cf7b808e750da9d499d (patch)
tree88f8ec5571b1e47d32a940e03e291caa15034592 /kotlin.html.markdown
parent2fa414912e2fd9bdcd8fd5aa6df59e70f24dac06 (diff)
[Kotlin/en] Add when smartcast example (#2418)
Diffstat (limited to 'kotlin.html.markdown')
-rw-r--r--kotlin.html.markdown8
1 files changed, 8 insertions, 0 deletions
diff --git a/kotlin.html.markdown b/kotlin.html.markdown
index 4c2f3082..fe2811c5 100644
--- a/kotlin.html.markdown
+++ b/kotlin.html.markdown
@@ -317,6 +317,14 @@ fun helloWorld(val name : String) {
println(smartCastExample(0)) // => false
println(smartCastExample(true)) // => true
+ // Smartcast also works with when block
+ fun smartCastWhenExample(x: Any) = when (x) {
+ is Boolean -> x
+ is Int -> x > 0
+ is String -> x.isNotEmpty()
+ else -> false
+ }
+
/*
Extensions are a way to add new functionality to a class.
This is similar to C# extension methods.