匹配、案例与逻辑?

发布于 2024-12-20 00:30:59 字数 665 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2024-12-27 00:30:59
case i if i > 0 => Ok( ... )

因此,要区分 01 到 1011 到 20

case 0 =>
case i if i >=  1 && i <= 10 =>
case i if i >= 11 && i <= 20 =>

但是我猜想 if-< code>else if-else 块更具可读性。

case i if i > 0 => Ok( ... )

So to distinguish between 0, 1 to 10 and 11 to 20:

case 0 =>
case i if i >=  1 && i <= 10 =>
case i if i >= 11 && i <= 20 =>

But then I guess an if-else if-else block is more readable.

失与倦" 2024-12-27 00:30:59

这就是所谓的守卫:

case x if (x > 0) => OK ...

This is called guards:

case x if (x > 0) => OK ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文