jeudi 9 février 2017

Use Enumeration inside of Regex pattern matching

Vote count: 0

I have written this code

object Foo extends Enumeration {
  val X = Value("X")
  val Y = Value("Y")
}

val regex = """([A-Z]+)(\d+)""".r
val input = "ABC123"

input match {
  case regex(x, num) if x == Foo.X.toString => // do something
  case regex(x, num) if x == Foo.Y.toString => // do something
  case _ => // throw error
}

This works but is a little too verbose. I tried to change it to

input match {
  case regex(`Foo.X.toString`, num) => // do something
  case regex(`Foo.Y.toString`, num) => // do something
  case _ => // throw error
}

But this does not compile. Apartently those ``` work for "vals" but not for enumerations.

asked 13 secs ago

Let's block ads! (Why?)



Use Enumeration inside of Regex pattern matching

Aucun commentaire:

Enregistrer un commentaire