TL;DR: scalac apparently doesn't believe in exhaustiveness of value set (true
, false
) for a boolean, so pattern matching on a boolean in Scala emits a branch for "neither true nor false". (I won't even go into it emitting comparison to 1 and 0 instead of using ifeq
or ifne
instructions, or those goto
s that jump to the very next instruction.)
This scala code:
final class X {
final def foo(b: Boolean) =
b match {
case true => 10
case false => 11
}
}
after treated with javap:
scalac X.scala
javap -c -p X
shows:
public final int foo(boolean);
Code:
0: iload_1
1: istore_3
2: iconst_1
3: iload_3
4: if_icmpne 13
7: bipush 10
9: istore_2
10: goto 42
13: goto 16
16: iconst_0
17: iload_3
18: if_icmpne 27
21: bipush 11
23: istore_2
24: goto 42
27: goto 30
30: new #13 // class scala/MatchError
33: dup
34: iload_3
35: invokestatic #19 // Method scala/runtime/BoxesRunTime.boxToBoolean:(Z)Ljava/lang/Boolean;
38: invokespecial #23 // Method scala/MatchError."<init>":(Ljava/lang/Object;)V
41: athrow
42: iload_2
43: ireturn