在案例类的特征中定义“复制”方法

2023-12-31

给出简化的代码示例:

sealed trait A {

  val c1: String
  val c2: Int

  def copy[Z <: A](src: File) : Z

}

case class B(c1: String, c2: Int, src: File) extends A

case class C(c1: String, c2: Int, c3: MyClass, src: File) extends A

我如何定义copy特质中的方法A那么它会匹配为案例类和“目标”文件生成的文件吗?给定的定义会进行类型检查并抱怨缺少方法copy在B类和C类中。


scala 编译器不会为定义名为 copy 的方法的案例类生成复制方法。

scala -Xprint:typer -e "sealed trait A { def copy[T <: A](s: String):T };  case class B(x: Int, y:Int) extends A"

outputs:

/var/folders/fm/fm4b21vj6jl995ywlrd99t49tjthjc/T/scalacmd3413244935208502669.scala:1: error: class B needs to be abstract, since method copy in trait A of type [T <: this.A](s: String)T is not defined
sealed trait A { def copy[T <: A](s: String):T };  case class B(x: Int, y: Int) extends A
                                                              ^
one error found
[[syntax trees at end of                     typer]] // scalacmd3413244935208502669.scala
package <empty> {
  object Main extends scala.AnyRef {
    def <init>(): Main.type = {
      Main.super.<init>();
      ()
    };
    def main(argv: Array[String]): Unit = {
      val args: Array[String] = argv;
      {
        final class $anon extends scala.AnyRef {
          def <init>(): anonymous class $anon = {
            $anon.super.<init>();
            ()
          };
          sealed abstract trait A extends scala.AnyRef {
            def copy[T >: Nothing <: this.A](s: String): T
          };
          case class B extends AnyRef with this.A with Product with Serializable {
            <caseaccessor> <paramaccessor> private[this] val x: Int = _;
            <stable> <caseaccessor> <accessor> <paramaccessor> def x: Int = B.this.x;
            <caseaccessor> <paramaccessor> private[this] val y: Int = _;
            <stable> <caseaccessor> <accessor> <paramaccessor> def y: Int = B.this.y;
            def <init>(x: Int, y: Int): this.B = {
              B.super.<init>();
              ()
            };
            override <synthetic> def productPrefix: String = "B";
            <synthetic> def productArity: Int = 2;
            <synthetic> def productElement(x$1: Int): Any = x$1 match {
              case 0 => B.this.x
              case 1 => B.this.y
              case _ => throw new IndexOutOfBoundsException(x$1.toString())
            };
            override <synthetic> def productIterator: Iterator[Any] = runtime.this.ScalaRunTime.typedProductIterator[Any](B.this);
            <synthetic> def canEqual(x$1: Any): Boolean = x$1.$isInstanceOf[this.B]();
            override <synthetic> def hashCode(): Int = {
              <synthetic> var acc: Int = -889275714;
              acc = Statics.this.mix(acc, x);
              acc = Statics.this.mix(acc, y);
              Statics.this.finalizeHash(acc, 2)
            };
            override <synthetic> def toString(): String = ScalaRunTime.this._toString(B.this);
            override <synthetic> def equals(x$1: Any): Boolean = B.this.eq(x$1.asInstanceOf[Object]).||(x$1 match {
  case (_: this.B) => true
  case _ => false
}.&&({
              <synthetic> val B$1: this.B = x$1.asInstanceOf[this.B];
              B.this.x.==(B$1.x).&&(B.this.y.==(B$1.y)).&&(B$1.canEqual(B.this))
            }))
          };
          <synthetic> private object B extends scala.runtime.AbstractFunction2[Int,Int,this.B] with Serializable {
            def <init>(): this.B.type = {
              B.super.<init>();
              ()
            };
            final override <synthetic> def toString(): String = "B";
            case <synthetic> def apply(x: Int, y: Int): this.B = new B(x, y);
            case <synthetic> def unapply(x$0: this.B): Option[(Int, Int)] = if (x$0.==(null))
              scala.this.None
            else
              Some.apply[(Int, Int)](Tuple2.apply[Int, Int](x$0.x, x$0.y));
            <synthetic> private def readResolve(): Object = $anon.this.B
          }
        };
        {
          new $anon();
          ()
        }
      }
    }
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在案例类的特征中定义“复制”方法 的相关文章

随机推荐

  • 将 KeyEvent 对象列表转换为字符串

    我正在开发一个需要能够使用外部 USB 或蓝牙键盘楔的应用程序 然后 我捕获 KeyEvent 并根据需要缓存它们 开始 结束标记 因此 此时我有需要将其转换为字符串的 KeyEvent 列表 所以我需要将这些有序的 KeyEvent 转换
  • 使用 Ganglia 进行 Spark 监控

    我正在测试 Apache Spark 框架 我需要监视有关集群的某些方面 例如网络和资源 Ganglia 看起来是满足我需要的一个不错的选择 然后 我发现Spark支持Ganglia Spark监控网页上有这样的信息 要安装 Ganglia
  • 谷歌云功能美国中部时区?

    我想知道什么时区云函数代码会像 EST CST MST PST 这样运行 我部署了函数来获取它 但它返回了2018 12 05T17 54 00 207Z并且不应用任何美国时区 不是在美国运行吗 export const dateLog f
  • vb.net 从通配符获取文件名列表

    我有字符串 c debug txt 在 Debug 文件夹中有几个 txt 文件 例如 test1 txt test2 txt test3 txt 如何从此字符串 c debug txt 获取通配符文件数组 a 0 c debug test
  • 快速搜索排序向量中大于 x 的最小值

    快速意味着比 O N 更好 这与 find 的能力一样好 我知道有ismembc and ismembc2 但我认为它们都不是我正在寻找的 我阅读了文档 他们似乎正在寻找会员equal到 x 但我想要索引第一个值较大 than x 现在如果
  • C++ Boost read_json 崩溃,我有 #define BOOST_SPIRIT_THREADSAFE

    最近其他人升级了我们的 boost 库 现在当 read json 函数被大量调用时我会看到这个 coredump 以前从未见过这个 我在代码中有 define BOOST SPIRIT THREADSAFE 这就是为什么以前没有发生这种情
  • TomEE + CXF配置参考

    所以我试图找到任何参考文档或使用这三个的重要示例 我发现的 最好的 是 TomEE OpenEJB JAXRS 重构 http rmannibucau wordpress com 2013 01 09 tomeeopenejb jaxrs
  • VS 2019 自定义 ASP.NET Core 项目模板

    我试图弄清楚如何 或者是否 可以创建一个自定义 Visual Studio 项目模板 该模板挂钩到 Visual Studio 2019 中可用的现有 ASP NET Core Web 应用程序模板 我想做的是类似于 madskristen
  • 使用 ApplicationViewBoundsMode.UseCoreWindow Windows Phone 8.1 时显示状态栏

    我的 Windows Phone 8 1 应用程序底部位于软件导航键下方 我用ApplicationViewBoundsMode UseCoreWindow使页面停留在软件导航键上方 但结果是页面的内容到达了状态栏下方 状态栏变得透明 有什
  • 禁用 Chromium Embedded 3 (DCEF3) 中的上下文菜单

    我试图在 Chromium Embedded DCEF3 窗口中禁用鼠标右键 上下文菜单 但我没有得到 我没有找到任何设置来本地执行此操作 例如 我可以禁用 查看源代码 我正在使用下面的代码 但我真正想要的是禁用上下文菜单 或者不希望它出现
  • 如何控制 UISlider Value Changed 事件频率?

    我正在编写一个 iPhone 应用程序 它使用两个 uislider 来控制使用 coreBluetooth 发送的值 如果我快速移动滑块 一个值会在接收器处冻结 大概是因为 值更改 事件触发得太频繁 导致写入命令堆积起来并最终被丢弃 如何
  • Spring、tomcat中限制http application/json请求体的大小

    我想限制接受的 application json http 请求正文的大小 因此 不可能将多个兆字节的 json 发送到我的应用程序 然后对其进行处理并使我的应用程序运行很长时间 我在这里读到 没有现成的解决方案可以做到这一点 Spring
  • 为 Apache2 + FastCGI 设置启用数据压缩

    这个问题与我之前的帖子相关 Android ICS 的 CSS 加载问题 https stackoverflow com questions 11575189 css loading issue with android ics 我遇到问题
  • 用新文件修补被子

    我正在尝试创建一个新的被子补丁 仅添加一个文件 不幸的是该文件似乎被忽略了 I did quilt new some patch and quilt add some file 我可以看到该文件quilt files然后 但是当我刷新时 我
  • SQL Server 不会打开 Visual Studio 创建的数据库

    我在 Visual Studio 中构建了一个 ASP Net 站点 并在其中创建了数据库 mdf 文件位于 APP data 文件夹中 现在 当我想要在客户站点中进行部署时 我会考虑在不打开 Visual Studio 的情况下维护数据库
  • 为什么 Windows、Linux 等汇编语言之间存在差异?

    我对所有这些低级的东西 汇编语言都比较陌生 并且想了解更多细节 为什么 Linux 和 Windows 汇编语言之间存在差异 据我了解 当我编译 C 代码时 操作系统并不真正生成纯机器或汇编代码 它会生成依赖于操作系统的二进制代码 但是为什
  • 为什么 Kotlin 中 `+` 之前没有换行,而 `.` 之前没有换行?

    我试图理解这个答案 https stackoverflow com a 44180583 481061 https stackoverflow com a 44180583 481061特别是这一部分 如果语句的第一行是有效语句 则它将不起
  • 获取 pandas 数据框中的字符串数据列并将其拆分为单独的列

    我有一个 pandas 数据框 来自从 CSV 读取的数据 一列用于组的名称 而另一列包含一个字符串 看起来像一个列表 如下所示 Group Followers biebers u user1 u user2 u user3 catladi
  • FireFox 和 Google Chrome 中的 JavaScript 对象字面量行为不同? [复制]

    这个问题在这里已经有答案了 我已将错误范围缩小到以下在 Google Chrome 中产生不同行为的代码集 示例代码 http jnvxxx appspot com rpc static server key testjs html htt
  • 在案例类的特征中定义“复制”方法

    给出简化的代码示例 sealed trait A val c1 String val c2 Int def copy Z lt A src File Z case class B c1 String c2 Int src File exte