为什么以下模块无法在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
}
}
更新1
在按照答案中的建议后,下一个问题是编译持续时间太长。有趣的是,如果我删除上面的2个case语句,我会得到以下编译器错误:
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
}
}
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)
答案 0 :(得分:3)
您正在尝试使用util.matching.Regex
作为提取器模式。你可以这样做,因为这个类定义了一个unapplySeq
方法。在模式匹配中,对提取器模式的引用必须以大写字母开头。
val PVoid = """\s*void\s*""".r
def mapType(t: String): String = t.trim match {
case PVoid() => "Unit"
case x => x
}
<强>更新强>
以下是您的代码的完整,可编译版本:
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
}
}
}
将模式匹配分解为解决方法错误#1113。