'As' function for constants

This commit is contained in:
Natalia Ukhorskaya
2013-12-17 12:52:18 +04:00
parent b4ad149e77
commit 7a9d822070
9 changed files with 136 additions and 14 deletions
@@ -0,0 +1,38 @@
fun box(): String {
if (check(1, { it as Int }) == "OK") return "fail 1"
if (check(1, { it as Byte }) != "OK") return "fail 2"
if (check(1, { it as Short }) != "OK") return "fail 3"
if (check(1, { it as Long }) != "OK") return "fail 4"
if (check(1, { it as Char }) != "OK") return "fail 5"
if (check(1, { it as Double }) != "OK") return "fail 6"
if (check(1, { it as Float }) != "OK") return "fail 7"
if (check(1.0, { it as Int }) != "OK") return "fail 11"
if (check(1.0, { it as Byte }) != "OK") return "fail 12"
if (check(1.0, { it as Short }) != "OK") return "fail 13"
if (check(1.0, { it as Long }) != "OK") return "fail 14"
if (check(1.0, { it as Char }) != "OK") return "fail 15"
if (check(1.0, { it as Double }) == "OK") return "fail 16"
if (check(1.0, { it as Float }) != "OK") return "fail 17"
if (check(1f, { it as Int }) != "OK") return "fail 21"
if (check(1f, { it as Byte }) != "OK") return "fail 22"
if (check(1f, { it as Short }) != "OK") return "fail 23"
if (check(1f, { it as Long }) != "OK") return "fail 24"
if (check(1f, { it as Char }) != "OK") return "fail 25"
if (check(1f, { it as Double }) != "OK") return "fail 26"
if (check(1f, { it as Float }) == "OK") return "fail 27"
return "OK"
}
fun check<T>(param: T, f: (T) -> Unit): String {
try {
f(param)
}
catch (e: ClassCastException) {
return "OK"
}
return "fail"
}
@@ -0,0 +1,27 @@
fun box(): String {
if ((1 as? Int) == null) return "fail 1"
if ((1 as? Byte) != null) return "fail 2"
if ((1 as? Short) != null) return "fail 3"
if ((1 as? Long) != null) return "fail 4"
if ((1 as? Char) != null) return "fail 5"
if ((1 as? Double) != null) return "fail 6"
if ((1 as? Float) != null) return "fail 7"
if ((1.0 as? Int) != null) return "fail 11"
if ((1.0 as? Byte) != null) return "fail 12"
if ((1.0 as? Short) != null) return "fail 13"
if ((1.0 as? Long) != null) return "fail 14"
if ((1.0 as? Char) != null) return "fail 15"
if ((1.0 as? Double) == null) return "fail 16"
if ((1.0 as? Float) != null) return "fail 17"
if ((1f as? Int) != null) return "fail 21"
if ((1f as? Byte) != null) return "fail 22"
if ((1f as? Short) != null) return "fail 23"
if ((1f as? Long) != null) return "fail 24"
if ((1f as? Char) != null) return "fail 25"
if ((1f as? Double) != null) return "fail 26"
if ((1f as? Float) == null) return "fail 27"
return "OK"
}