我在 Scala 2.10.3 中使用值类(扩展 AnyVal 的类),但是在将它们用作抽象方法的参数时遇到了一个奇怪的编译器错误。如以下示例所示:
class ValueClass(val x: Int) extends AnyVal
trait Test {
def foo(v: ValueClass): Int
}
new Test {
override def foo(v: ValueClass): Int = 1
}
编译器吐出以下错误:
error: bridge generated for member method foo: (v: ValueClass)Int in anonymous class $anon
which overrides method foo: (v: ValueClass)Int in trait Test
clashes with definition of the member itself;
both have erased type (v: Int)Int
override def foo(v: ValueClass): Int = 1
为什么这不起作用?有没有办法将值类传递给抽象方法?