17

有没有办法依赖在特征中的案例类中定义的方法?例如,复制:以下不起作用。不过,我不确定为什么。

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]
                      ^
4

3 回答 3

17

一种解决方案是声明您的 trait 必须应用到具有复制方法的类:

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)
}

(不幸的是,您不能在复制方法中使用隐式参数,因为类型声明中不允许隐式声明)

那么你的声明就可以了:

case class L(val x: String, val y: String) extends K[L]

(在 REPL scala 2.8.1 中测试)

您的尝试不起作用的原因在其他用户提出的解决方案中进行了解释:您的copy声明阻止了“ case copy”方法的生成。

于 2011-03-18T10:25:32.867 回答
5

我想在 trait 中使用名称为副本的方法会指示编译器不在案例类中生成方法副本 - 所以在您的示例中,方法副本没有在案例类中实现。下面是在 trait 中实现方法副本的简短实验:

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
于 2011-03-17T17:11:03.230 回答
1

您可以使用 $scala -Xprint:typer 运行 repl。使用参数 -Xprint:typer,您可以看到创建 trait 或类时究竟发生了什么。您将从输出中看到未创建方法“复制”,因此编译器要求您自己定义它。

于 2011-03-17T17:51:19.597 回答