Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways in which identifiers of Scala are formed

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the identifiers of Scala?". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

There are two ways to form identifiers in Scala: alphanumeric and operators. Scala has very flexible rules for constituting identifiers. In addition to these two, you will see that there are other two.

Alphanumeric identifiers: alphanumeric identifier starts with a letter or underscore and can be followed by a letter, number, or underscore.' The $'character is also treated as a letter, but is reserved as an identifier generated by the Scala compiler. Identifiers in user programs should not contain the'$'character, although they can be compiled; doing so may result in name collisions with identifiers generated by the Scala compiler.

Scala follows the hump style of Java, which is called hump: camel case because identifiers are made up of embedded words with uppercase initials. Identifier conventions, such as toString and HashSet. Although underlining is legal within identifiers, it is not commonly used in Scala programs, in part to maintain consistency with Java, but also because underscores have many other non-identifier uses in Scala code. Therefore, * avoid using identifiers such as to_string,__init__, or name_. Fields, method parameters, local variables, and hump names of functions should start with lowercase letters such as: length,flatMap, and s. Hump names for classes and attributes should start with capital letters, such as BigInt,List and UnbalancedTreeMap.

Be careful

One result of an underscore at the end of an identifier is that, for example, if you try to write a definition like "val name_: Int = 1", you will receive a compiler error. The compiler will assume that you normally define a variable called "name_:". To make it compile, you will need to insert an extra space before the colon, such as "val name_: Int = 1".

The difference between the habits of Scala and Java lies in constant names. In Scala, the word constant is not the same as val. Although val does remain the same after initialization, it is still a variable. For example, the method parameter is val, but these val can represent different values each time the method is called. And constants last longer. For example, scala.Math.Pi is defined as a double value very close to the real number , indicating the ratio of the circumference to its diameter. This value is unlikely to change, so Pi is obviously a constant. You can also use constants to give some magic numbers in your code: a name for the value to be used by magic number: text values are not interpretable and will be extremely bad if they appear in multiple places. You may also need to define the constants used in pattern matching, and the use case is described in Section 15.2. In Java, it is customary that constant names are all capitalized, separating words such as MAX_VALUE or PI with an underscore. In Scala, it is customary that letters must be capitalized. Therefore, Java-style constant names, such as X_OFFSET, can also be used in Scala, but the convention for Scala is that constants are also used in hump style, such as XOffset.

Operator identifier: operator identifier consists of one or more operator characters. Operator characters are printable ASCII characters such as +,:,?, ~, or #. More precisely, operator characters belong to the Unicode set of mathematical symbols (Sm) or other symbols (So), or 7-bit ASCII characters that are not letters, numbers, parentheses, square braces, curly braces, single or double quotes, or underscores, periods, semicolons, colons, or fallback characters. Here are some examples of operator identifiers:

+ +::

< ?>

:->

The Scala compiler converts internal "shatter" operator identifiers to legitimate embedded'$'Java identifiers. For example, the identifier:-> will be expressed internally as $colon$minus$greater. If you want to access this identifier from Java code, you should use this internal expression.

Operator identifiers in Scala can become arbitrarily long, so there are some small differences between Java and Scala. In Java, enter x

< -y将会被拆分成四个词汇符号,所以写成x < - y也没什么不同。Scala里,< -将被作为一个标识符拆分,而得到x < - y。如果你想要得到***种解释,你要在'< '和'-'字符间加一个空格。这大概不会是实际应用中的问题,因为没什么人会在Java里写x< -y的时候不注意加空格或括号的。 混合标识符:mixed identifier由字母数字组成,后面跟着下划线和一个操作符标识符。例如,unary_+被用做定义一元的'+'操作符的方法名。或者,myvar_=被用做定义赋值操作符的方法名。多说一句,混合标识符格式myvar_=是由Scala编译器产生的用来支持属性:property的;第十八章进一步说明。 文本标识符:literal identifier是用反引号`...`包括的任意字串。如: `x` `< clinit>

``yield`

The idea is that you can put any run-time approved string between backquotes as an identifier. The result is always the Scala identifier. This rule is valid even if the name enclosed between backquotes is a Scala reserved word. Accessing static yield methods in Java's Thread class is a typical use case. You can't write Thread.yield () because yield is a reserved word for Scala. However, you can still refer to the name of the method in backquotes, such as Thread.`yield` ().

This is the end of the content of "how Scala identifiers are formed". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report