匹配、案例与逻辑?
def searchEquipmentCategory(category: String) = Action {
val equipment = Equipment.searchByCategory(category)
equipment.size match {
case 0 => NotFound(views.html.helpers.notfound("Equipment not found for category :" + category))
case (_ > 0) => Ok(views.html.equipment.index(equipment, capitalize(category)))
}
}
是否可以将逻辑放入 match case 语句中?
我已经到处搜索,但找不到任何文档。我只想在情况为 0 且数字超过 0 时执行一件事。
在这种情况下使用 _
默认值可以正常工作,但如果我想做 3 件事怎么办?
- if number == 0
- 如果数字在 1 到 10 之间
- 如果数字在 11 到 20 之间
也许我试图对大小写做太多事情。
感谢您的帮助。
def searchEquipmentCategory(category: String) = Action {
val equipment = Equipment.searchByCategory(category)
equipment.size match {
case 0 => NotFound(views.html.helpers.notfound("Equipment not found for category :" + category))
case (_ > 0) => Ok(views.html.equipment.index(equipment, capitalize(category)))
}
}
Is it possible put logic in a match case statement?
I've searched everywhere and can't find any documentation. I just want to have if the case is 0 do one thing if the number is over 0.
Using the _
default works fine in that situation, but what if I wanted to do 3 things?
- if number == 0
- if the number is between 1 and 10
- if the number is between 11 and 20
Maybe I'm trying to do too much with case.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,要区分
0
、1 到 10
和11 到 20
:但是我猜想
if
-< code>else if-else
块更具可读性。So to distinguish between
0
,1 to 10
and11 to 20
:But then I guess an
if
-else if
-else
block is more readable.这就是所谓的守卫:
This is called guards: