是否可以在匹配案例中使用隐式函数?
Is it possible to use a implicit function in match case?
我有这样的枚举。
object SortCountryField extends Enumeration {
type SortCountryField = Value
val countryName = Value("country_name")
val countryStatus = Value("country_status")
}
我在 match-case
中使用这个 SortCountryField
枚举。
这里我每次都需要转换为String。
为了方便起见,我正在尝试 implicit
转换器从 SortCountryField.{Value}
中提取 String
但是,当我在以下匹配案例中使用隐式函数时,我最终遇到了编译器错误。
'myString' match{
case SortCountryField.countryName.toString => //Some operations
case SortCountryField.countryStatus.toString => //another operation
}
错误日志:-
found : mypackage.ConstantUtils.SortCountryField.Value
[error] required: String
[error] case SortCountryField.countryStatus => //my-operations
我认为你最好在比赛中使用枚举,例如:
SortCountryField withName <your_string> match {
case SortCountryField.countryName => //Some operations
case SortCountryField.countryStatus => //another operation
}
如果您的字符串有时不匹配任何字段,那么您可以轻松地将其包装在 Try
中,如以下代码所示:
Try(SortCountryField withName <your_string>) match {
case Success(SortCountryField.countryName) => //Some operations
case Success(SortCountryField.countryStatus) => //another operation
case _ => //another operation
}
您还可以这样做:
'myString' match{
case x if x == SortCountryField.countryName.toString => //Some operations
case x if x == SortCountryField.countryStatus.toString => //another operation
}
你的隐式转换器是什么样子的?
我猜你有转换器 SortCountryField => String
,但你需要 SortCountryField.Value => String
个转换器。
在您的枚举中添加以下函数:
implicit def toString(value: Value): String = value.toString
在匹配中使用以下内容:
val countryStaus:String = SortCountryField.countryStatus
'myString' match {
case `countryStatus` => //Some operations
case _ => // Another operation
}
我有这样的枚举。
object SortCountryField extends Enumeration {
type SortCountryField = Value
val countryName = Value("country_name")
val countryStatus = Value("country_status")
}
我在 match-case
中使用这个 SortCountryField
枚举。
这里我每次都需要转换为String。
为了方便起见,我正在尝试 implicit
转换器从 SortCountryField.{Value}
String
但是,当我在以下匹配案例中使用隐式函数时,我最终遇到了编译器错误。
'myString' match{
case SortCountryField.countryName.toString => //Some operations
case SortCountryField.countryStatus.toString => //another operation
}
错误日志:-
found : mypackage.ConstantUtils.SortCountryField.Value
[error] required: String
[error] case SortCountryField.countryStatus => //my-operations
我认为你最好在比赛中使用枚举,例如:
SortCountryField withName <your_string> match {
case SortCountryField.countryName => //Some operations
case SortCountryField.countryStatus => //another operation
}
如果您的字符串有时不匹配任何字段,那么您可以轻松地将其包装在 Try
中,如以下代码所示:
Try(SortCountryField withName <your_string>) match {
case Success(SortCountryField.countryName) => //Some operations
case Success(SortCountryField.countryStatus) => //another operation
case _ => //another operation
}
您还可以这样做:
'myString' match{
case x if x == SortCountryField.countryName.toString => //Some operations
case x if x == SortCountryField.countryStatus.toString => //another operation
}
你的隐式转换器是什么样子的?
我猜你有转换器 SortCountryField => String
,但你需要 SortCountryField.Value => String
个转换器。
在您的枚举中添加以下函数:
implicit def toString(value: Value): String = value.toString
在匹配中使用以下内容:
val countryStaus:String = SortCountryField.countryStatus
'myString' match {
case `countryStatus` => //Some operations
case _ => // Another operation
}