Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有自动类型转换的Scala模式匹配变量绑定_Scala_Pattern Matching - Fatal编程技术网

带有自动类型转换的Scala模式匹配变量绑定

带有自动类型转换的Scala模式匹配变量绑定,scala,pattern-matching,Scala,Pattern Matching,在scala中,我们可以在类型上进行模式匹配。因此,可以将变量绑定到类型为的匹配模式。现在,有界变量的类型为Any val a: Any = "hello" a match { case v @ String { v.length() // not working } } 您的语法不正确!!!,应该是这样的: val a: Any = "hello" a match { case v: String => v.length() } 使用

在scala中,我们可以在类型上进行模式匹配。因此,可以将变量绑定到类型为的匹配模式。现在,有界变量的类型为
Any

val a: Any = "hello"
a match {
     case v @ String {
          v.length() // not working
     }
}

您的语法不正确!!!,应该是这样的:

val a: Any = "hello"
a match {
     case v: String => v.length()
}

使用
匹配类型,并将
=>
与下一个

匹配您的语法不正确!!!,应该是这样的:

val a: Any = "hello"
a match {
     case v: String => v.length()
}

使用
匹配类型,并使用下一个

按照@chengpohi中的语法对类型进行模式匹配

绑定操作符
@
用于引用模式匹配中提取的数据的(子)结构。例如在

("hello",123) match {
  case t @ (fst: String, snd: Int) => println(s"got tuple $t")
  case _                           =>
}

标签
t
指的是整个元组,不必表示
fst
snd

以按照@chengpohi中公开的语法对类型进行模式匹配

绑定操作符
@
用于引用模式匹配中提取的数据的(子)结构。例如在

("hello",123) match {
  case t @ (fst: String, snd: Int) => println(s"got tuple $t")
  case _                           =>
}
标签
t
指的是整个元组,不必表示
fst
snd