从大型参数列表实例化案例 Class

Instantiating a Case Class from a Large Parameter List

伙计们,当我有一个很大的参数列表时,我遇到了麻烦,但是当我有一些完美的工作时,有人知道它可能是什么吗?

小参数列表,还行

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

scala> val params = Foo(1, "bar", 3.14).productIterator.toList
params: List[Any] = List(1, bar, 3.14)

scala> Foo.getClass.getMethods.find(x => x.getName == "apply" && x.isBridge).get.invoke(Foo, params map (_.asInstanceOf[AnyRef]): _*).asInstanceOf[Foo]
res0: Foo = Foo(1,bar,3.14)

scala> Foo(1, "bar", 3.14) == res0
res1: Boolean = true

当我有一个非常大的参数列表时,它会显示以下错误:

scala> case class Foo(a1: String,a2: String,a3: String,a4: String,a5: String,a6: String,a7: String,a8: String,a9: String,a10: String,a12: String,a13: String,a14: String,a15: String,a16: String,a17: String,a18: String,a19: String,a20: String,a21: String,a22: String,a23: String,a24: String)
defined class Foo

scala> val params2 = Foo("bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar","bar").productIterator.toList  
params2: List[Any] = List(bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar, bar)

scala> val test = Foo.getClass.getMethods.find(x => x.getName == "apply" && x.isBridge).get.invoke(Foo, params2 map (_.asInstanceOf[AnyRef]): _*).asInstanceOf[Foo]
java.util.NoSuchElementException: None.get
  at scala.None$.get(Option.scala:347)
  at scala.None$.get(Option.scala:345)
  ... 46 elided

大小写限制为 22 类。更大的案例 类 仍然可以编译,但有一些限制。

https://underscore.io/blog/posts/2016/10/11/twenty-two.html