What will be printed out as the result of the following code execution?

object Main extends App {
  val str = "one"
  val digit =
    str match {
      case "one" => 1
      case "two" => 2
      case "three" => 3
      case _ => -1
    }
  println(digit)
}
Explanation
Match expressions result in a value, which is the reason why digit variable can be assigned to the match expression. Another difference from Java is that there are no breaks at the end of each alternative. Instead the break is implicit, and there is no fall through from one alternative to the next, which is why only one result will be returned (in our case it's 1)
Source: Match expressions

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Scala
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты