从一个大参数列表中实例化一个案例类。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从一个大参数列表中实例化一个案例类。相关的知识,希望对你有一定的参考价值。
伙计们,当我有一个大的参数列表时,我就会遇到麻烦,但当我有几个参数完美地工作时,有人知道可能是什么原因吗?
小参数列表,好的
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
答案
case类有22个限制。更大的case类仍然可以编译,但是有一些限制。
https:/underscore.ioblogposts20161011twenty-two.html。
以上是关于从一个大参数列表中实例化一个案例类。的主要内容,如果未能解决你的问题,请参考以下文章