带有自动类型转换的Scala模式匹配变量绑定
在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() } 使用
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