1

我想知道是否有一种方法可以直接从 for comprehension 实例化案例类(在这种情况下,我使用 scalaz 验证函子),而无需手动输入来自 for comprehension 的参数

如果你有这样的案例类

  case class Rawr(id:Option[Long],int:Int)

然后假设你做这样的事情

  val test = for {
    none <- None.successNel
    anotherVariable <- 3.successNel
  } yield Rawr.apply _ //This doesn't work, just here to make it compile. 
  // I want to instantiate a Rawr automatically with the variables
  // in the for comprehension instead of having to manually put in
  // none and anotherVariable 

所以我终于可以像这样使用它了

  test match {
    case Success(p) => {
      //Should get a case class instance here i.e. p = Rawr(None,3)
    }
  }

问题是,我没有得到 p 的案例类实例(显然 p 是不正确的,因为我只是使用了 apply)。

我问这个的原因是我有提供 20 多个变量的理解,并且必须将每个变量输入到案例类实例化中会产生很多样板文件

4

0 回答 0