How to use switch/case (simple pattern matching) in Scala?

前端 未结 2 1051
旧巷少年郎
旧巷少年郎 2021-02-04 23:47

I\'ve found myself stuck on a very trivial thing :-]

I\'ve got an enum:

 object Eny extends Enumeration {
      type Eny = Value
      val FOO, BAR, WOOZ         


        
2条回答
  •  孤街浪徒
    2021-02-05 00:40

    I suspect the code you are actually using is not FOO, but foo, lowercase, which will cause Scala to just assign the value to foo, instead of comparing the value to it.

    In other words:

    x match {
      case A => // compare x to A, because of the uppercase
      case b => // assign x to b
      case `b` => // compare x to b, because of the backtick
    }
    

提交回复
热议问题