match with string numbers - unreachable code

前端 未结 1 1625
孤城傲影
孤城傲影 2021-01-28 02:41

New to scala, and can\'t seem to get my match expression to work. I\'ve read about the differences between how the statement is evaluated (e.g. a new variable as opposed to one

相关标签:
1条回答
  • 2021-01-28 03:15

    Either of these would work:

    // declared inside of object
    val numberOne = "+17201234567"
    val numberTwo = "+17201235678"
    
    def returnSomething(number: String): String = number match {
      case `numberOne` => "my first number"
      case `numberTwo` => "my second number"
      case _ => "a default number"
    }
    
    
    // declared inside of object
    val NumberOne = "+17201234567"
    val NumberTwo = "+17201235678"
    
    def returnSomething(number: String): String = number match {
      case NumberOne => "my first number"
      case NumberTwo => "my second number"
      case _ => "a default number"
    }
    

    But you said you tried and it did not work. So, what exactly went wrong?

    0 讨论(0)
提交回复
热议问题