mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-10 08:31:29 +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
29 lines
486 B
Kotlin
Vendored
29 lines
486 B
Kotlin
Vendored
fun foo(x: Int): Int {
|
|
return when (x) {
|
|
1, 1, 2 -> 1001
|
|
1, 2 -> 1002
|
|
1 -> 1003
|
|
2 -> 1004
|
|
3 -> 1005
|
|
else -> 1006
|
|
}
|
|
}
|
|
|
|
fun box(): String {
|
|
if (foo(0) != 1006)
|
|
return "0: " + foo(0)
|
|
|
|
if (foo(1) != 1001)
|
|
return "1: " + foo(1)
|
|
|
|
if (foo(2) != 1001)
|
|
return "2: " + foo(2)
|
|
|
|
if (foo(3) != 1005)
|
|
return "3: " + foo(3)
|
|
|
|
if (foo(4) != 1006)
|
|
return "4: " + foo(4)
|
|
|
|
return "OK"
|
|
} |