Scala的if else方式有两个条件变量

时间:2016-08-04 20:38:25

标签: scala if-statement coding-style modularity

我有以下Java代码段(其中ab是期​​货):

if (a.isEmpty && b.isEmpty) func(list)
else if (a.isEmpty) func(list, b)
else if (b.isEmpty) func(a, list)
else func(a, list, b)

我有函数'func'的所有实现。 有没有一种正确的方法在Scala中写这个或者这还不错?

1 个答案:

答案 0 :(得分:1)

假设ab是列表,这似乎与list相关:

(a, b) match {
  case (Nil, Nil) => func(list)
  case (Nil, _)   => func(list, b)
  case (_, Nil)   => func(a, list)
  case _          => func(a, b, list)
}