2016-04-22 54 views
0

說我們有一個類:從各指標應用構造函數參數列表中的

case class Header(a:String, b:String, c:String, d:String) //in reality its 16 arguments 

val a = ("1","2","3","4") //or list. I think tuple is more useful as we can keep track of arity 

我想是構造函數的參數適用元組a的所有值Header。例如:

Header(a._0,a._1,a._2,a._3) //or 
Header.curried(a._0)(a._1)(a._2)(a._3) 

上面的過程中有太多的鍋爐板,因爲參數必須手動輸入。有沒有一種方法可以簡單地將元組參數應用於循環或基於其索引的構造函數中?

回答

3

您可以使用tupled的情況下,類同伴apply方法轉換爲功能1,簡單地採取與所需的所有ARGS一個元組:

case class Header(a:String, b:String, c:String, d:String) 
val a = ("1","2","3","4") 

Header.tupled(a) // -> Header(1,2,3,4) 
// The above is short for the line below, the result is the same 
(Header.apply _).tupled(a) // -> Header(1,2,3,4) 
+0

啊哈。我忘記了tupled! – Jatin