如何从List创建案例类

时间:2017-04-17 06:54:23

标签: scala

val list = List(A(None,None,Some("dummyText")),
           "DummmyText", None, None, None, None, None, None, Some("322"), 
          Some("1233"))

我需要将其转换为case class

case class Dummy(code: A, someValue1: String, someValue2: Option[B] = None,
                            someValue3: Option[B] = None, someValue4: Option[B] = None,
                            someValue5: Option[B] = None, someValue6: Option[A] = None,
                            someValue7: Option[List[A]] = None, someValue8: Option[String] = None, someValue9: Option[String] = None)

我试过这个Instantiating a case class from a list of parameters

但不行,因为我的List有子类型。

是否可以转换List这样可以转换为case class

1 个答案:

答案 0 :(得分:2)

你的参数列表中有一个错误,如果你仔细引用Instantiating a case class from a list of parameters,它确实有效:

val params = List(
    A(None,None,Some("dummyText")),
    "DummmyText",
    None,
    None,
    None,
    None,
    None,
    None,
    Some("1233")
)

case class Dummy(code: A,
             someValue1: String,
             someValue2: Option[B] = None,
             someValue3: Option[B] = None,
             someValue4: Option[B] = None,
             someValue5: Option[B] = None,
             someValue6: Option[A] = None,
             someValue7: Option[List[A]] = None,
             someValue8: Option[String] = None
)


Dummy.
    getClass.
    getMethods.
    find(x => x.getName == "apply" && x.isBridge).
    get.
    invoke(Dummy, params map (_.asInstanceOf[AnyRef]): _*).
    asInstanceOf[Dummy]