Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala how to access match case value in each case block

Tags:

scala

I want to access to the case value in the case block

 val a = "hello" 
  a match {
    case "hello" | "Hi" =>
      println("hello") // how can I access the right case value? Hello or Hi?
    case _ =>
      print("NA")
  }
like image 226
CS1999 Avatar asked Feb 01 '26 02:02

CS1999


1 Answers

You can reference the matched value like so:

a match {
    case str @ ("hello" | "Hi") => println(str)
    case _ => print("NA")
}
like image 180
Ryan Stull Avatar answered Feb 02 '26 19:02

Ryan Stull