gpt4 book ai didi

scala - 无法复制具有重复参数的案例类

转载 作者:行者123 更新时间:2023-12-04 22:56:35 25 4
gpt4 key购买 nike

为什么不能使用带有重复参数的case类的copy方法?

例如,为什么这段代码的最后一行给了我错误?

case class A(i: Int)
case class B(i: Int*)

val a = A(1).copy(i = 2)
val b1 = B(i = Seq(4, 5): _*)
val b2 = B(2, 3).copy(i = Seq(4, 5): _*)

value copy is not a member of B

最佳答案

根据斯卡拉 specs copy编译器不会为具有重复参数的案例类生成方法。

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.

关于scala - 无法复制具有重复参数的案例类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43092184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com