有没有办法依赖特征中case类中定义的方法?例如,复制:以下不起作用.不过,我不知道为什么.
trait K[T <: K[T]] {
val x: String
val y: String
def m: T = copy(x = "hello")
def copy(x: String = this.x, y: String = this.y): T
}
case class L(val x: String, val y: String) extends K[L]
Run Code Online (Sandbox Code Playgroud)
得到:
error: class L needs to be abstract, since method copy in trait K of type
(x: String,y: String)L is not defined
case class L(val x: String, val y: String) extends K[L]
^
Run Code Online (Sandbox Code Playgroud)
Nic*_*las 15
解决方案是声明必须使用复制方法将特征应用于类:
trait K[T <: K[T]] {this: {def copy(x: String, y: String): T} =>
val x: String
val y: String
def m: T = copy(x = "hello", y)
}
Run Code Online (Sandbox Code Playgroud)
(遗憾的是,您不能在复制方法中使用隐式参数,因为类型声明中不允许使用隐式声明)
然后你的声明是好的:
case class L(val x: String, val y: String) extends K[L]
Run Code Online (Sandbox Code Playgroud)
(在REPL scala 2.8.1中测试)
您的尝试不起作用的原因在其他用户提出的解决方案中进行了解释:您的copy
声明会阻止生成" case copy
"方法.
我认为在特征中使用名称为 copy 的方法会指示编译器不在案例类中生成方法副本 - 因此在您的示例中,方法副本未在您的案例类中实现。下面是在特征中实现方法复制的简短实验:
scala> trait K[T <: K[T]] {
| val x: String
| val y: String
| def m: T = copy(x = "hello")
| def copy(x: String = this.x, y: String = this.y): T = {println("I'm from trait"); null.asInstanceOf[T]}
| }
defined trait K
scala> case class L(val x: String, val y: String) extends K[L]
defined class L
scala> val c = L("x","y")
c: L = L(x,y)
scala> val d = c.copy()
I'm from trait
d: L = null
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6111 次 |
最近记录: |