Jetbo
Jetbo

Reputation: 95

How to return pattern-matched element?

I want to match a String returned from someFunction and have the default case return the String from the someFunction.

someFunction(input) match {
  case "123" => "234"
  case "234" => "345"
  case _ => _
}

Where case _ => _ returns what someFunction(input) actually returns. Is this possible?

Upvotes: 1

Views: 37

Answers (1)

dhg
dhg

Reputation: 52681

You just need to name the default case:

someFunction(input) match {
  case "123" => "234"
  case "234" => "345"
  case x => x
}

Upvotes: 6

Related Questions