Pattern match never optimizes for switch/case statement

168 views
Skip to first unread message

Maurício Linhares

unread,
Apr 30, 2013, 10:44:21 PM4/30/13
to scala-user
I have this pattern match (
https://github.com/mauricio/postgresql-async/blob/master/src/main/scala/com/github/mauricio/async/db/postgresql/MessageEncoder.scala#L46
) that matches only on Byte values but when I add the @switch it says:

"could not emit switch to @switch annotated match"

What am I missing here?

Just FYI, what I have tried already and did not work:

* Moving this constant to a Java interface and using public static
final byte fields (I have also tried making them int instead of byte)
* Marking the fields as final val at the Scala Message companion object
* Marking the fields as @inline at the companion object

Not sure what I should try now.

-
Maurício Linhares
http://mauricio.github.io/ - http://twitter.com/#!/mauriciojr

Som Snytt

unread,
May 1, 2013, 12:16:52 AM5/1/13
to Maurício Linhares, scala-user
Try final val without the Byte.

scala> object Colors {
     | final val Red = 'r'
     | final val Green = 'g'
     | final val Blue = 'b'
     | }
defined object Colors

scala> case class Colorful(hue: Byte)
defined class Colorful

scala> def f(c: Colorful) = (c.hue: @switch) match {
     | case Red => 1
     | case Green => 2
     | case Blue => 3
     | }
f: (c: Colorful)Int

scala> :javap #f
  public int f(Colorful);
    flags: ACC_PUBLIC
    Code:
      stack=3, locals=3, args_size=2
         0: aload_1      
         1: invokevirtual #20                 // Method Colorful.hue:()B
         4: istore_2     
         5: iload_2      
         6: lookupswitch  { // 3
                      98: 52
                     103: 56
                     114: 60
                 default: 40
            }
        40: new           #22                 // class scala/MatchError








--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Maurício Linhares

unread,
May 1, 2013, 11:01:06 PM5/1/13
to Som Snytt, scala-user
That was it, thanks!
Reply all
Reply to author
Forward
0 new messages