0
在scala中我们可以对类型进行模式匹配。所以有可能将一个变量绑定到匹配的类型。眼下有界变量的类型为Any
Scala模式匹配变量绑定与自动类型转换
val a: Any = "hello"
a match {
case v @ String {
v.length() // not working
}
}
在scala中我们可以对类型进行模式匹配。所以有可能将一个变量绑定到匹配的类型。眼下有界变量的类型为Any
Scala模式匹配变量绑定与自动类型转换
val a: Any = "hello"
a match {
case v @ String {
v.length() // not working
}
}
你的语法不正确!!!,它应该是这样的:
val a: Any = "hello"
a match {
case v: String => v.length()
}
使用:
匹配类型,=>
下一个block
要在类型上模式匹配,请遵循@chengpohi中公开的语法。
绑定运算符@
用于指代在模式匹配中提取的数据的(子)结构。例如,在
("hello",123) match {
case t @ (fst: String, snd: Int) => println(s"got tuple $t")
case _ =>
}
标签t
指的是整个的元组,不必表示fst
和snd
。
该死的。谢谢 !! –