- 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.
- HornetQ Messaging Developer’s Guide
- Vue.js設計與實現
- Progressive Web Apps with React
- Building a Game with Unity and Blender
- C#程序設計(慕課版)
- x86匯編語言:從實模式到保護模式(第2版)
- Python編程:從入門到實踐
- SQL經典實例(第2版)
- Arduino可穿戴設備開發
- C++程序設計教程(第2版)
- Anaconda數據科學實戰
- 物聯網系統架構設計與邊緣計算(原書第2版)
- HTML5 Canvas核心技術:圖形、動畫與游戲開發
- RESTful Web API Design with Node.js
- Raspberry Pi Robotic Projects