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

前端 未结 2 1053
旧巷少年郎
旧巷少年郎 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:36

    The following code works fine for me: it produces 6

    object Eny extends Enumeration {
      type Eny = Value
      val FOO, BAR, WOOZLE, DOOZLE = Value
    }
    
    import Eny._
    
    class EnumTest {
        def doit(en: Eny) = {
            val num = en match {
              case FOO => 4
              case BAR => 5
              case WOOZLE => 6
              case DOOZLE => 7
            }       
    
            num
        }
    }
    
    object EnumTest {
        def main(args: Array[String]) = {
            println("" + new EnumTest().doit(WOOZLE))
        }
    }
    

    Could you say how this differs from your problem please?

提交回复
热议问题