gpt4 book ai didi

scala - Scala案例类命名参数

转载 作者:行者123 更新时间:2023-12-04 17:02:24 25 4
gpt4 key购买 nike

scala中是否可以(或在最近的功能中)使用命名参数创建案例类?我的意思是在构造函数中使用命名参数构造案例类对象。

我的案例课有很多领域。具有很多未命名参数的构造函数很容易出错。

我想得到(目前无法正常工作的示例):

case class X(x0:String, x1:Int, x2:String)
val x = X(x0="Xstring", x1=12, x2="x2String")

解决方法是,我们可以提供如下摘录所示的工厂方法,但这是丑陋的解决方案:
case class X(x0:String, x1:Int, x2:String)
object X {
private nullX = X(null, null, null)
def createX = nullX.copy _
}

val x = X.createX(x0="Xstring", x1=12, x2="x2String")

有什么线索吗? :)

编辑:
这是某些旧版本intellij idea scala插件中的错误。 Plase不要再投票了;)

最佳答案

命名和默认参数在Scala 2.8中引入

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.

scala> case class X(x0:String, x1:Int, x2:String)
defined class X

scala> val x = X(x0="Xstring", x1=12, x2="x2String")
x: X = X(Xstring,12,x2String)

关于scala - Scala案例类命名参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12037563/

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