Unified Types
In Scala, all values have a type, including numerical values and functions. The diagram below illustrates a subset of the type hierarchy.
Scala Type Hierarchy
Any is the supertype of all types, also called the top type. It defines certain universal methods such as equals, hashCode, and toString. Any has two direct subclasses: AnyVal and AnyRef.
AnyVal represents value types. There are nine predefined value types and they are non-nullable: Double, Float, Long, Int, Short, Byte, Char, Unit, and Boolean. Unit is a value type which carries no meaningful information. There is exactly one instance of Unit which can be declared literally like so: (). All functions must return something so sometimes Unit is a useful return type.
AnyRef represents reference types. All non-value types are defined as reference types. Every user-defined type in Scala is a subtype of AnyRef. If Scala is used in the context of a Java runtime environment, AnyRef corresponds to java.lang.Object.
Here is an example that demonstrates that strings, integers, characters, boolean values, and functions are all of type Any just like every other object:
val list: List[Any] = List( "a string", 732, // an integer 'c', // a character true, // a boolean value () => "an anonymous function returning a string" ) list.foreach(element => println(element))
It defines a value list of type List[Any]. The list is initialized with elements of various types, but each is an instance of scala.Any, so you can add them to the list.
Here is the output of the program:
a string 732 c true <function>
Type Casting
Value types can be cast in the following way:
Note that Long to Float conversion is deprecated in new versions of Scala, because of the potential precision lost.
For example:
val x: Long = 987654321 val y: Float = x.toFloat // 9.8765434E8 (note that some precision is lost in this case) val face: Char = '☺' val number: Int = face // 9786
Casting is unidirectional. This will not compile:
val x: Long = 987654321 val y: Float = x.toFloat // 9.8765434E8 val z: Long = y // Does not conform
You can also cast a reference type to a subtype. This will be covered later in the tour.
Nothing and Null
Nothing is a subtype of all types, also called the bottom type. There is no value that has type Nothing. A common use is to signal non-termination such as a thrown exception, program exit, or an infinite loop (i.e., it is the type of an expression which does not evaluate to a value, or a method that does not return normally).
Null is a subtype of all reference types (i.e. any subtype of AnyRef). It has a single value identified by the keyword literal null. Null is provided mostly for interoperability with other JVM languages and should almost never be used in Scala code. We’ll cover alternatives to null later in the tour.
网址:Unified Types https://m.mxgxt.com/news/view/1648151
相关内容
杨炳铎Easy to crook multi
Easy to crooked translation quality
User interface design
The mobile user interface is designed to be crooked
Easy to download the official website
哈登+保罗=晃断脚踝,“灯泡组合”首秀德鲁联赛大杀四方
陈碧连
使用Plotly创建带有回归趋势线的时间序列可视化图表数据
珊瑚疾病的主要类型、生态危害及其与环境的关系
随便看看
- 作为还会去看ljj演唱会的歌迷来为他说几句话吧
- 半塘主义:在这个充满数字化互动的时代,明星与粉丝之间的关系愈加复杂而紧密。蔡徐坤,一位在年轻一代中拥有广泛影响力的艺人,最近因一张壁纸而引发了热议。这张来自米兰的照片,不仅仅是一幅美丽的画面,更是一种情感的载体,令无数粉丝为之心动。 想象一下,当你打开手机屏幕,映入眼帘的便是蔡徐坤那一抹淡淡的微笑,背景是米兰那迷人的街景。这样的画面,仿佛让人瞬间置身于那座充满艺术与历史的城市。许多粉丝在社交媒体...
- 张子枫工作室解散后援会引发粉丝担忧:明星与粉丝关系再思考
- 娱乐圈真相揭秘:3大明星互动背后的10大算计
- 同样是老板带艺人,看陈赫和邓超与员工相处的细节,差距一目了然

