Dom*_*mra 16 scala pattern-matching
我有一个我要表达的声明,在C伪代码中看起来像这样:
switch(foo):
case(1)
if(x > y) {
if (z == true)
doSomething()
}
else {
doSomethingElse()
}
return doSomethingElseEntirely()
case(2)
essentially more of the same
Run Code Online (Sandbox Code Playgroud)
使用scala模式匹配语法是一种很好的方法吗?
Tom*_*cek 44
如果要处理多个条件,在一个match声明中,您还可以使用守卫,允许您指定的情况下,附加条件:
foo match {
case 1 if x > y && z => doSomething()
case 1 if x > y => doSomethingElse()
case 1 => doSomethingElseEntirely()
case 2 => ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23459 次 |
| 最近记录: |