Scala - 复杂条件模式匹配
我有一个我想表达的声明,在 C 伪代码中,它看起来像这样:
switch(foo):
case(1)
if(x > y) {
if (z == true) {
doSomething()
}
else {
doSomethingElse()
}
}
return doSomethingElseEntirely()
case(2)
// essentially more of the same
Is a good way possible with the scala 模式匹配语法?
I have a statement I want to express, that in C pseudo-code would look like this:
switch(foo):
case(1)
if(x > y) {
if (z == true) {
doSomething()
}
else {
doSomethingElse()
}
}
return doSomethingElseEntirely()
case(2)
// essentially more of the same
Is a nice way possible with the scala pattern matching syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想在单个
match
语句中处理多个条件,您还可以使用 guards 来为案例指定其他条件:If you want to handle multiple conditions in a single
match
statement, you can also use guards that allow you to specify additional conditions for a case: