//@是为了给模式匹配起个变量名,一般格式为:variableName@pattern,示例:
object VariableTest {

  def main(args: Array[String]): Unit = {
    val list = List(123456)

    list match {
      //right其实就代表了一个集合,元素为3,4,5,6
      case List(_, _, right@_*) => println(right)
      case _ =>
    }

    list match {
      //这种写法错误
      //case l:List(_, _, _*) => println(right)

      //正确写法,下面两种输出结果一致
      case list@List(_, _, _*) => println(list)
      case list: List[_] => println(list)
    }
  }

} 
 

 

相关文章:

  • 2021-08-31
  • 2022-12-23
  • 2021-11-16
  • 2022-12-23
  • 2021-06-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-22
  • 2021-09-29
  • 2021-10-01
  • 2022-01-06
  • 2021-08-19
  • 2021-05-11
相关资源
相似解决方案