Scala 方法和值名称 [英] Scala method and values names

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

问题描述

为什么编译失败:

scala> val a? = true
<console>:1: error: illegal start of simple pattern
   val a? = true
          ^

这有效吗?

scala>  val a_? = true
a_?: Boolean = true

推荐答案

根据 Scala 语言规范(看 2.8 后,怀疑事情发生了很大变化):

According to the Scala language specification (looking at 2.8, doubt things have changed much since):

idrest ::= {字母 |数字} [`_' op]

idrest ::= {letter | digit} [`_' op]

也就是说,标识符可以以字母或数字开头,后跟下划线字符,以及更多的运算符字符.这使得诸如 foo_!@! 之类的标识符成为有效标识符.另请注意,标识符也可能仅包含一串运算符字符.考虑以下 REPL 会话:

That is, an identifier can start with a letter or a digit followed by an underscore character, and further operator characters. That makes identifiers such as foo_!@! valid identifiers. Also, note that identifiers may also contain a string of operator characters alone. Consider the following REPL session:

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_16).

scala> val +aff = true
<console>:1: error: illegal start of simple pattern
val +aff = true
^

scala> val ??? = true
???: Boolean = true

scala> val foo_!@! = true
foo_!@!: Boolean = true

scala> val %^@%@ = true
%^@%@: Boolean = true

scala> val ^&*!%@ = 42
^&*!%@: Int = 42

希望这能回答您的问题.

Hope this answers your question.

这篇关于Scala 方法和值名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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