mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-16 08:31:35 +00:00
Effectively, the following when structure:
when (s) {
s1, s2 -> e1,
s3 -> e2,
s4 -> e3,
...
else -> e
}
is implemented as:
when (s.hashCode()) {
h1 -> {
if (s == s1)
e1
else if (s == s2)
e1
else if (s == s3)
e2
else
e
}
h2 -> if (s == s3) e2 else e,
...
else -> e
}
where s1.hashCode() == s2.hashCode() == s3.hashCode() == h1,
s4.hashCode() == h2.
A tableswitch or lookupswitch is used for the hash code lookup.
Change-Id: I087bf623dbb4a41d3cc64399a1b42342a50757a6
20 lines
454 B
Kotlin
Vendored
20 lines
454 B
Kotlin
Vendored
fun foo(x: Int): String {
|
|
return when (x) {
|
|
2_147_483_647 -> "MAX"
|
|
-2_147_483_648 -> "MIN"
|
|
else -> "else"
|
|
}
|
|
}
|
|
|
|
fun box(): String {
|
|
if (foo(0) != "else")
|
|
return "0: " + foo(0).toString()
|
|
|
|
if (foo(Int.MAX_VALUE) != "MAX")
|
|
return "Int.MAX_VALUE: " + foo(Int.MAX_VALUE).toString()
|
|
|
|
if (foo(Int.MIN_VALUE) != "MIN")
|
|
return "Int.MIN_VALUE: " + foo(Int.MIN_VALUE).toString()
|
|
|
|
return "OK"
|
|
} |