我有一个案例类:
case class Part1(id: Int)
case class Part2(id: Int)
case class Part3(id: Int)
以上所有内容都包含在下面的部分类中:
case class Parts(part1: Option[Part1], part2: Option[Part2], part3: Option[Part3])
现在我的代码中有:
case (maybePart1, maybePart2, maybePart3) =>
Parts(maybePart1, maybePart2, maybePart3)
但我真正想做的是,如果所有这些都是None,则返回None,否则返回我正在做的事情。
我该怎么做?
答案 0 :(得分:6)
如果案例全部为None
,只需添加另一个案例陈述:
case (None, None, None) => None
case (maybePart1, maybePart2, maybePart3) =>
Some(Parts(maybePart1, maybePart2, maybePart3))