mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
[Kotlin/en] Add when smartcast example (#2418)
This commit is contained in:
parent
2fa414912e
commit
98bb8f8432
@ -317,6 +317,14 @@ fun helloWorld(val name : String) {
|
|||||||
println(smartCastExample(0)) // => false
|
println(smartCastExample(0)) // => false
|
||||||
println(smartCastExample(true)) // => true
|
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.
|
Extensions are a way to add new functionality to a class.
|
||||||
This is similar to C# extension methods.
|
This is similar to C# extension methods.
|
||||||
|
Loading…
Reference in New Issue
Block a user