通过与数组进行模式匹配的多重赋值不适用于大写值
阅读这个答案后我尝试玩我自己使用了这个很好的功能,发现当我这样做时可以
scala> val Array(a,b,n) = "XXX,YYY,ZZZ".split(",")
a: java.lang.String = XXX
b: java.lang.String = YYY
n: java.lang.String = ZZZ
,但对于大写命名变量则不行:
scala> val Array(a,b,N) = "XXX,YYY,ZZZ".split(",")
<console>:9: error: not found: value N
val Array(a,b,N) = "XXX,YYY,ZZZ".split(",")
这种行为的原因是什么?
UPD 实际上,元组分配也是如此:
scala> val (a,b,N) = (1,2,3)
<console>:9: error: not found: value N
val (a,b,N) = (1,2,3)
After reading this answer I've tried to play with this nice feature by myself and found out that it is ok when I'm do
scala> val Array(a,b,n) = "XXX,YYY,ZZZ".split(",")
a: java.lang.String = XXX
b: java.lang.String = YYY
n: java.lang.String = ZZZ
But is not ok with uppercase named variable:
scala> val Array(a,b,N) = "XXX,YYY,ZZZ".split(",")
<console>:9: error: not found: value N
val Array(a,b,N) = "XXX,YYY,ZZZ".split(",")
What is the reason of such behavior?
UPD
Actually, the same thing with tuples assigment:
scala> val (a,b,N) = (1,2,3)
<console>:9: error: not found: value N
val (a,b,N) = (1,2,3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Scala 将其视为匹配模式的常量。注意:
要提取值的变量必须以小写字母开头。
Scala treats it as a constant against which to match the pattern. Observe:
The variables in which you want to extract the values must begin with a lower-case letter.