Skip to content

Instantly share code, notes, and snippets.

@ozcanzaferayan
Created November 12, 2018 08:27
Show Gist options
  • Select an option

  • Save ozcanzaferayan/802518b2d68576a3deb00aa60d2b12e0 to your computer and use it in GitHub Desktop.

Select an option

Save ozcanzaferayan/802518b2d68576a3deb00aa60d2b12e0 to your computer and use it in GitHub Desktop.
Kotlin Nedir 2: Tip kontrolü - smart casting
fun test(x: Any): Int {
if (x is String) {
return x.length
} else if (x is Array<*>) {
return x.size
} else if (x is Int) {
return x
} else {
throw IllegalArgumentException()
}
}
fun main(args: Array<String>) {
println(test("40")) // Çıktı: 2
println(test(40)) // Çıktı: 40
var arr = Array(5, { i -> i * 10 })
println(test(arr)) // Çıktı:5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment