In the following code segment,
val line = ... // a list of String array 
line match {
  case Seq("Foo", ... ) => ...
  case Seq("Bar", ... ) => ...
  ...
I change the above code to the followings:
object Title extends Enumeration  {
  type Title = Value
  val Foo, Bar, ... = Value
}
val line = ... // a list of String array 
line match {
  case Seq(Title.Foo.toString, ... ) => ...
  case Seq(Title.Bar.toString, ... ) => ...
  ...
And, I get an error:
stable identifier required, but com.abc.domain.enums.Title.Foo.toString found.
What will be a right way to replace a string in the case statement?
A function call cannot be used as a stable identifier pattern (that is it's not a stable identifier).
In particular:
A path is one of the following.
The empty path
ε(which cannot be written explicitly in user programs).
C.this, whereCreferences a class. The path this is taken as a shorthand for C.this where C is the name of the class directly enclosing the reference.
p.xwherepis a path andxis a stable member ofp. Stable members are packages or members introduced by object definitions or by value definitions of non-volatile types.
C.super.xorC.super[M].xwhereCreferences a class andxreferences a stable member of the super class or designated parent classMofC. The prefix super is taken as a shorthand forC.superwhereCis the name of the class directly enclosing the reference.A stable identifier is a path which ends in an identifier.
Thus, You cannot use a function call like xyz.toString in a pattern.
To make it stable you can assign it to a val. If the identifier starts with a lower case letter, you will need to enclose it in backticks (`) to avoid shadowing it:
 val line = Seq("Foo") // a list of String array
 val FooString = Title.Foo.toString
 val fooLowerCase = Title.Foo.toString
 line match {
   case Seq(FooString) => ???
   // case Seq(fooLowerCase) (no backticks) matches any sequence of 1 element, 
   // assigning it to the "fooLowerCase" identifier local to the case
   case Seq(`fooLowerCase`) => ???
 }
You can use guards though:
line match {
  case Seq(x) if x == Title.Foo.toString => ???
}
toString is a function and functions cannot be used for pattern matching.
I think Enumeration is probably not what you are looking for. 
To match a string you could
object Title {
  val Foo = "Foo"
  val Bar = "Bar"
}
line match {
  case Seq(Title.Foo, ...) => ???
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With