Match “fallthrough”: executing same piece of code for more than one case?

前端 未结 4 903
死守一世寂寞
死守一世寂寞 2020-12-17 07:38

What is the Scala\'s way to write the following code:

 int i;

 switch(i) {
   case 1:  
         a();
         break;

   case 2:
   case 15:
        b();
          


        
4条回答
  •  余生分开走
    2020-12-17 08:13

    According to this conversation there is no fallthrough, but you can make use of |.

    This should do the trick:

    i match {
      case 1  => a    
      case 2 | 15 => b
                     c
      case _ => foo        
    } 
    

提交回复
热议问题