依赖特质案例类的方法

Aar*_*ken 17 scala case-class

有没有办法依赖特征中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"方法.

  • 虽然这在极少数情况下有效,但大多数复制方法都依赖于抽象类型,这些抽象类型在细化之外是不允许的. (4认同)
  • 此外,方法“copy”的参数数量必须与子类中使用的案例类参数的数量完全匹配。因此,如果 L 有 3 个参数而不是 2 个参数,则特征 K 将与案例类 L 不兼容。 (3认同)

Mar*_*usz 5

我认为在特征中使用名称为 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)

  • 解决方案如果只是说没有解决方案:( (5认同)