1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-09 08:16:58 +02:00

[Kotlin/en] Add when smartcast example (#2418)

This commit is contained in:
Sergey Mashkov
2016-10-05 14:34:08 +03:00
committed by ven
parent 2fa414912e
commit 98bb8f8432

View File

@@ -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.