首页 文章

如何将match语句的输出保存到变量中?

提问于
浏览
0

我有一段代码我在其中使用了模式匹配我在所有情况下都使用了map,我想得到什么 Map 给变量的输出 . 以下是我的代码:

override def run():List[Option[Student]] =
StudentDataCache.get(surname) match {
  case Some(i) => i.otherSiblings.map(siblings =>
    StudentDataCache.get(siblings) match {
      case Some(i) => Some(i)
      case None=> getStudentFromDatabase(siblings)
    }
  )
  case None =>
    getStudentFromDatabase(surname).get.otherSiblings.map(siblings => StudentDataCache.get(siblings) match {
        case Some(i) => Some(i)
        case None=> getStudentFromDatabase(siblings)
      }
    )
}

case语句内部的输出是List [Option [Student]],是否有办法将其转换为变量,因为我想将此列表转换为单个对象,因为HystrixCommand执行输出不支持List作为输出 . 我想将其转换为StudentListing(val列表:List [Option [Student]])

1 回答

  • 0

    只是...将其分配给值/变量:

    override def run(): StudentListing = {
      val result = StudentDataCache.get(surname) match { /* same code*/ }
      StudentListing(result) // or however you wrap it into a StudentListing...
    }
    

    与Scala中的任何其他表达式一样,匹配表达式被计算为值 - 您可以使用此值执行任何操作 .

相关问题