Scala 语法奇怪与 :: 并需要小写 [英] Scala syntax strangeness with :: and requiring lower case

查看:15
本文介绍了Scala 语法奇怪与 :: 并需要小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是应该发生的吗?

scala> val myList = List(42)
myList: List[Int] = List(42)

scala> val s2 :: Nil = myList
s2: Int = 42

scala> val S2 :: Nil = myList
<console>:8: error: not found: value S2
       val S2 :: Nil = myList
           ^

它似乎区分大小写.错误或功能"?

It appears to be case sensitive. Bug or 'feature'?

推荐答案

区分大小写.在匹配模式中,以大写字母开头(或用反引号引用)的标识符被视为对已定义值的引用,而不是新绑定.

It is case-sensitive. In a match pattern, an identifier beginning with a capital letter (or quoted by backticks) is treated as a reference to a defined value, not as a new binding.

这让很多人感到惊讶,阅读 Scala 语言规范并不完全清楚.最相关的位是 “可变模式”...

This catches a lot of people by surprise, and it isn't entirely obvious from reading the Scala language specification. The most relevant bits are “variable patterns” ...

变量模式 x 是一个以小写字母开头的简单标识符.它匹配任何值,并将变量名绑定到该值.

A variable pattern x is a simple identifier which starts with a lower case letter. It matches any value, and binds the variable name to that value.

... 和 “稳定的标识符模式”:

... and “stable identifier patterns”:

为了解决与变量模式的句法重叠,稳定的标识符模式可能不是以小写字母开头的简单名称.

To resolve the syntactic overlap with a variable pattern, a stable identifier pattern may not be a simple name starting with a lower-case letter.

相关问题:

这篇关于Scala 语法奇怪与 :: 并需要小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆