Created
November 12, 2018 08:27
-
-
Save ozcanzaferayan/802518b2d68576a3deb00aa60d2b12e0 to your computer and use it in GitHub Desktop.
Kotlin Nedir 2: Tip kontrolü - smart casting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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