Why can't a variable be a stable identifier?

后端 未结 3 1682
滥情空心
滥情空心 2020-12-10 10:11

The following

def mMatch(s: String) = {
    var target: String = \"a\"
    s match {
        case `target` => println(\"It was \" + target)
        case _         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 10:41

    There's nothing to stop you just turning your var into a val before using it in the match:

    def mMatch(s: String) = {
        var target: String = "a"
        val x = target
        s match {
            case `x` => println("It was " + target)
            case _ => println("It was something else")
        }
    }
    

    works perfectly fine.

提交回复
热议问题