微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

scala – 案例类扩展特征不能使用副本

我试试

trait Foo[A] {
  def copy(int: Int): A
}

case class Bar(int: Int) extends Foo[Bar]

但我明白了

error: class Bar needs to be abstract,since method copy in trait Foo of type (int: Int)this.Bar is not defined

由于Bar是一个案例类,它会自动定义具有此签名的复制方法.

为什么Foo类不满足trait Bar定义的接口?

解决方法

我引用了 Scala’s specification

A method named copy is implicitly added to every case class unless the class already has a member (directly defined or inherited) with that name,or the class has a repeated parameter.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐