gpt4 book ai didi

scala - Play 带有枚举案例语句的框架 scala 模板

转载 作者:行者123 更新时间:2023-12-04 07:05:17 25 4
gpt4 key购买 nike

我有这个 scala 模板,想使用 case 语句根据匹配的枚举值呈现不同的 html。

我的模板是这样的:

@(tagStatus: String)

try {
TagStatus.withName(tagStatus) match {
case TagStatus.deployed => {<span class="label label-success">@tagStatus</span>}
case TagStatus.deployedPartially => {<span class="label label-important">@tagStatus</span>}
case TagStatus.deployedWithProblems => {<span class="label label-important">@tagStatus</span>}
}
} catch {
{<span class="label label-important">??</span>}
}

枚举看起来像这样:

object TagStatus extends Enumeration{
val deployed = Value("deployed")
val deployedWithProblems = Value("deployed_with_problems")
val deployedPartially = Value("deployed_partially")
}

当我运行它时,我得到:

Compilation error
')' expected but 'case' found.
In C:\...\statusTag.scala.html at line 8.
5 case TagStatus.deployed => {<span class="label label-success">@tagStatus</span>}
6 case TagStatus.deployedPartially => {<span class="label label-important">@tagStatus</span>}
7 case TagStatus.deployedWithProblems => {<span class="label label-important">@tagStatus</span>}
8 }
9 } catch {
10 {<span class="label label-important">??</span>}
11 }

我不知道这个错误信息是什么意思。

为了编译这个简单的代码片段,我缺少什么?

谢谢!

最佳答案

toString 与 match 不兼容,因此使用 withName 将 String 转换为枚举

你可以这样做 - 我不太确定 Play 语法:

TagsStatus.withName(tagStatus) match {
case TagStatus.deployed => {<span class="label label-success">@tagStatus</span>}
case TagStatus.deployedPartially => {<span class="label label-important">@tagStatus</span>}
case TagStatus.deployedWithProblems => {<span class="label label-important">@tagStatus</span>}
case _ => {<span class="label label-important">??</span>}
}

顺便说一句,关于 Scala pattern matching with lower case variable names 有一个常见的相关问题

关于scala - Play 带有枚举案例语句的框架 scala 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17895947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com