Instantiating a case class from a list of parameters

后端 未结 4 1511
春和景丽
春和景丽 2020-12-02 07:27

Given:

case class Foo(a: Int, b: String, c: Double)

you can say:

val params = Foo(1, \"bar\", 3.14).productIterator.toList
         


        
4条回答
  •  死守一世寂寞
    2020-12-02 08:11

    You could use pattern matching like:

    params match {                                   
     case List(x:Int, y:String, d:Double) => Foo(x,y,d)
    }
    

提交回复
热议问题