From 98bb8f8432e66c5721bb7cf7b808e750da9d499d Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Wed, 5 Oct 2016 14:34:08 +0300 Subject: [Kotlin/en] Add when smartcast example (#2418) --- kotlin.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) 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. -- cgit v1.2.3