scala中是否(或将在最近的特征中)可以创建具有命名参数的case类?我的意思是在构造函数中使用命名参数构造case类对象.
我的案例类有很多字段.具有许多未命名参数的构造函数容易出错.
我想得到(目前不工作的例子):
case class X(x0:String, x1:Int, x2:String)
val x = X(x0="Xstring", x1=12, x2="x2String")
Run Code Online (Sandbox Code Playgroud)
作为解决方法,我们可以提供工厂方法,如下面摘录节目,但这是丑陋的解决方案:
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")
Run Code Online (Sandbox Code Playgroud)
有线索吗?:)
编辑:这是intellij想法scala插件中的一些旧版本的错误.Plase不再投票;)
Bri*_*ith 12
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)
Run Code Online (Sandbox Code Playgroud)