依赖于特征中案例类的方法
有没有办法依赖特征中案例类中定义的方法?例如,复制:以下内容不起作用。但我不确定为什么。
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]
给出:
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]
^
Is there a way to rely on methods defined in case class in a trait? E.g., copy: the following doesn't work. I'm not sure why, though.
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]
Gives:
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]
^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案是声明您的特征必须应用于具有复制方法的类:(
不幸的是,您不能在复制方法中使用隐式参数,因为类型声明中不允许隐式声明)
那么您的声明就可以了
:(在 REPL scala 2.8.1 中进行了测试)
您的尝试不起作用的原因在其他用户提出的解决方案中进行了解释:您的
copy
声明阻止了“case copy”方法。
A solution is to declare that your trait must be applied to a class with a copy method:
(unfortunately you can not use implicit parameter in the copy method, as implicit declaration is not allowed in the type declaration)
Then your declaration is ok:
(tested in REPL scala 2.8.1)
The reason why your attempt does not work is explained in the solution proposed by other users: your
copy
declaration block the generation of the "case copy
" method.我认为在特征中使用名称为 copy 的方法会指示编译器不在案例类中生成方法副本 - 因此在您的示例中,方法副本未在您的案例类中实现。下面是在特征中实现方法复制的简短实验:
I suppose that having method with name copy in trait instructs compiler to not generate method copy in case class - so in your example method copy is not implemented in your case class. Below short experiment with method copy implemented in trait:
您可以使用 $scala -Xprint:typer 运行 repl。使用参数 -Xprint:typer 您可以看到创建特征或类时到底发生了什么。并且您将从输出中看到该方法“copy”未创建,因此编译器要求您自己定义它。
You can run repl with $scala -Xprint:typer. With parameter -Xprint:typer you can see what exactly happening when you create trait or class. And you will see from output that method "copy" not created, so compiler requests to define it by yourself.