Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala case class named parameters

Is there (or will in the nearest feature) in scala possible to create case class with named parameters? I mean to construct case class object using named parameters in constructor.

My case classes have a lot of fields. It is prone to error having constructors with a lot of not named parameters.

I would like to get (at the moment not working example):

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

As workaround we could provide factory method like below excerpt shows, but this is ugly solution:

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")

Any clues ? :)

EDIT: This was bug in intellij idea scala plugin some old version. Plase don't vote down any more ;)

like image 490
pawel.panasewicz Avatar asked Aug 20 '12 12:08

pawel.panasewicz


1 Answers

Named and default arguments were introduced in 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)
like image 62
Brian Smith Avatar answered Sep 28 '22 08:09

Brian Smith