Files
kotlin-fork/js/js.translator/testData/box/expression/when/externalEnumSubject.kt
T
Sergej Jaskiewicz 55ae6d1f3e [JS IR] Optimize pattern-matching of enums into comparing their ordinals
For this code:
```
enum class Season {
    WINTER,
    SPRING,
    SUMMER,
    AUTUMN
}

fun bar1(x : Season) : String {
    return when (x) {
        Season.WINTER, Season.SPRING -> "winter_spring"
        Season.SUMMER -> "summer"
        else -> "autumn"
    }
}
```

previously we generated this:
```
function foo(x) {
    var tmp0_subject = x;
    return (tmp0_subject.equals(Season_WINTER_getInstance())
        ? true
        : tmp0_subject.equals(Season_SPRING_getInstance()))
            ? 'winter_spring'
            : tmp0_subject.equals(Season_SUMMER_getInstance())
                ? 'summer'
                : 'autumn';
}
```

And now we generated this:
```
function bar2(x) {
  var tmp0_subject = x;
  var tmp0 = tmp0_subject._get_ordinal__0_k$();
  var tmp;
  switch (tmp0) {
    case 0:
    case 1:
      tmp = 'winter_spring';
      break;
    case 2:
      tmp = 'summer';
      break;
    case 3:
      tmp = 'autumn';
      break;
    default:
      noWhenBranchMatchedException();
      break;
  }
  return tmp;
}
```
2021-10-14 11:44:01 +00:00

17 lines
314 B
Kotlin
Vendored

// EXPECTED_REACHABLE_NODES: 1291
// CHECK_IF_COUNT: function=foo count=2
external enum class X {
A, B
}
fun box(): String {
if (foo(X.A) != "!") return "fail1: ${foo(X.A)}"
if (foo(X.B) != "@") return "fail2: ${foo(X.B)}"
return "OK"
}
fun foo(x: X) = when (x) {
X.A -> "!"
X.B -> "@"
}