- Learning Scala Programming
- Vikash Sharma
- 234字
- 2021-06-30 19:07:51
AnyVal
The latter, AnyVal, represents values not implemented as objects in the underlying runtime system. Scala has some predefined numeric and non-numeric value classes as shown in the class hierarchy.
It's possible to define user-defined value types as well, with some conditions applied. Remember I asked you to take a closer look at the source of the Int.scala file? You may have noticed that there are no val or var declarations in the class Int that extends AnyVal. This is one of the constraints of defining a subtype of AnyVal. One of the reasons you might consider defining an AnyVal type is to avoid object instantiation at runtime. Some of the constraints are:
- It must have a single val parameter that is the underlying representation. It means that if you declare class Num(val underlying: Int) extends AnyVal, then the compile time representation of it will be of type Num, but at runtime it'll be converted to Int, and methods defined within will be used as statics.
- It must define only def, no val, var, nested classes, traits, or objects.
- It can only extend universal traits, traits that extend only the super type Any.
- It can't be used for pattern-matching, or type tests.
Scala's implementation of AnyVal includes nine implementations. Out of them, Byte, Short, Int, Long, Char, Float, and Double are numeric value types, while Boolean and Unit are non-numeric types.
- Cocos2D-X權(quán)威指南(第2版)
- BeagleBone Media Center
- Elastic Stack應(yīng)用寶典
- Unity Game Development Scripting
- Android程序設(shè)計(jì)基礎(chǔ)
- 深入理解Elasticsearch(原書第3版)
- Python算法指南:程序員經(jīng)典算法分析與實(shí)現(xiàn)
- HTML5秘籍(第2版)
- Python開(kāi)發(fā)基礎(chǔ)
- 零基礎(chǔ)看圖學(xué)ScratchJr:少兒趣味編程(全彩大字版)
- UML軟件建模
- Learning C++ by Creating Games with UE4
- C#程序設(shè)計(jì)基礎(chǔ)入門教程
- Clojure for Finance
- C#網(wǎng)絡(luò)程序開(kāi)發(fā)(第二版)