开发者

Scala 2.8.0.RC2 compiler issue on pattern matching statement?

Why does the following module not compile on Scala 2.8.RC[1,2]?

object Test {

import util.matching.Regex._

val pVoid = """\s*void\s*""".r
val pVoidPtr = """\s*(const\s+)?void\s*\*\s*""".r
val pCharPtr = """\s*(const\s+)GLchar\s*\*\s*""".r
val pIntPtr = """\s*(const\s+)?GLint\s*\*\s*""".r
val pUintPtr = """\s*(const\s+)?GLuint\s*\*\s*""".r
val pFloatPtr = """\s*(const\s+)?GLfloat\s*\*\s*""".r
val pDoublePtr = """\s*(const\s+)?GLdouble\s*\*\s*""".r
val pShortPtr = """\s*(const\s+)?GLshort\s*\*\s*""".r
val pUshortPtr = """\s*(const\s+)?GLushort\s*\*\s*""".r
val pInt64Ptr = """\s*(const\s+)?GLint64\s*\*\s*""".r
val pUint64Ptr = """\s*(const\s+)?GLuint64\s*\*\s*""".r

def mapType(t: String): String = t.trim match {
  case pVoid() => "Unit"
  case pVoidPtr() => "ByteBuffer"
  case pCharPtr() => "CharBuffer"
  case pIntPtr() | pUintPtr() => "IntBuffer"
  case pFloatPtr() => "FloatBuffer"
  case pShortPtr() | pUshortPtr() => "ShortBuffer"
  case pDoublePtr() => "DoubleBuffer"
  case pInt64Ptr() | pUint64Ptr() => "LongBuffer"
  case x => x
}
}

UPDATE 1

After following the advice in the answer, the next issue is that the compilation lasts too long. Interestingly, If I remove 2 of the case statements above I get the follwing compiler error:

object Test {

    import util.matching.Regex._

    val PVoid = """\s*void\s*""".r
    va开发者_StackOverflow社区l PVoidPtr = """\s*(const\s+)?void\s*\*\s*""".r
    val PCharPtr = """\s*(const\s+)GLchar\s*\*\s*""".r
    val PIntPtr = """\s*(const\s+)?GLint\s*\*\s*""".r
    val PUintPtr = """\s*(const\s+)?GLuint\s*\*\s*""".r
    val PFloatPtr = """\s*(const\s+)?GLfloat\s*\*\s*""".r
    val PDoublePtr = """\s*(const\s+)?GLdouble\s*\*\s*""".r
    val PShortPtr = """\s*(const\s+)?GLshort\s*\*\s*""".r
    val PUshortPtr = """\s*(const\s+)?GLushort\s*\*\s*""".r
    val PInt64Ptr = """\s*(const\s+)?GLint64\s*\*\s*""".r
    val PUint64Ptr = """\s*(const\s+)?GLuint64\s*\*\s*""".r

    def mapType(t: String): String = t.trim match {
      case PVoid() => "Unit"
      case PVoidPtr() => "ByteBuffer"
      case PCharPtr() => "CharBuffer"
      case PIntPtr() | PUintPtr() => "IntBuffer"
      case PFloatPtr() => "FloatBuffer"
      case PShortPtr() | PUshortPtr() => "ShortBuffer"
      case PDoublePtr() => "DoubleBuffer"
      case PInt64Ptr() | PUint64Ptr() => "LongBuffer"
      case x => x
    }
}


Exception in thread "main" java.lang.Error:ch.epfl.lamp.fjbg.JCode$OffsetTooBigException: offset too big to fit in 16 bits: 43772
at ch.epfl.lamp.fjbg.JFieldOrMethod.writeTo(JFieldOrMethod.java:114)
at ch.epfl.lamp.fjbg.JClass.writeTo(JClass.java:315)


You are trying to use a util.matching.Regex as an Extractor Pattern. You can do this becuase this class defines an unapplySeq method. In pattern matching, references to extractor patterns must start with a capital letter.

val PVoid = """\s*void\s*""".r

def mapType(t: String): String = t.trim match {
  case PVoid() => "Unit"
  case x => x
}

UPDATE

Here's the full, compilable version of your code:

object Test {

    import util.matching.Regex._

    val PVoid = """\s*void\s*""".r
    val PVoidPtr = """\s*(const\s+)?void\s*\*\s*""".r
    val PCharPtr = """\s*(const\s+)GLchar\s*\*\s*""".r
    val PIntPtr = """\s*(const\s+)?GLint\s*\*\s*""".r
    val PUintPtr = """\s*(const\s+)?GLuint\s*\*\s*""".r
    val PFloatPtr = """\s*(const\s+)?GLfloat\s*\*\s*""".r
    val PDoublePtr = """\s*(const\s+)?GLdouble\s*\*\s*""".r
    val PShortPtr = """\s*(const\s+)?GLshort\s*\*\s*""".r
    val PUshortPtr = """\s*(const\s+)?GLushort\s*\*\s*""".r
    val PInt64Ptr = """\s*(const\s+)?GLint64\s*\*\s*""".r
    val PUint64Ptr = """\s*(const\s+)?GLuint64\s*\*\s*""".r

    def mapType(t: String): String = t.trim match {
      case PVoid() => "Unit"
      case PVoidPtr() => "ByteBuffer"
      case PCharPtr() => "CharBuffer"
      case PIntPtr() | PUintPtr() => "IntBuffer"
      case x => x match {
        case PFloatPtr() => "FloatBuffer"
        case PShortPtr() | PUshortPtr() => "ShortBuffer"
        case PDoublePtr() => "DoubleBuffer"
        case PInt64Ptr() | PUint64Ptr() => "LongBuffer"
        case x => x
      }
    }
}

The pattern match is split halfway to workaround bug #1113.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜