You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/tour/_posts/2017-02-13-unified-types.md
+31-7Lines changed: 31 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -9,19 +9,20 @@ categories: tour
9
9
num: 3
10
10
next-page: classes
11
11
previous-page: basics
12
+
prerequisite-knowledge: classes, basics
12
13
---
13
14
14
-
In Scala, all values are instances of a class, including numerical values and functions. The diagram below illustrates the class hierarchy.
15
+
In Scala, all values have a type, including numerical values and functions. The diagram below illustrates a subset of the type hierarchy.
15
16
16
-

17
+

17
18
18
-
## Scala Class Hierarchy ##
19
+
## Scala Type Hierarchy ##
19
20
20
-
The superclass of all classes `scala.Any` has two direct subclasses: `scala.AnyVal` and `scala.AnyRef`.
21
+
[`Any`](http://www.scala-lang.org/api/2.12.1/scala/Any.html) 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`.
21
22
22
-
`scala.AnyVal` represents value classes. All value classes are non-nullableand predefined; they correspond to the primitive types of Java-like languages. Note that the diagram above also shows implicit conversions between the value classes.
23
+
`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.
23
24
24
-
`scala.AnyRef` represents reference classes. All non-value classes are defined as reference class. Every user-defined class in Scala implicitly extends `scala.AnyRef`. If Scala is used in the context of a Java runtime environment, `scala.AnyRef` corresponds to `java.lang.Object`.
25
+
`AnyRef` represents reference types. All non-value types are defined as reference types. Anywhere an `AnyRef`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`.
25
26
26
27
Here is an example that demonstrates that strings, integers, characters, boolean values, and functions are all objects just like every other object:
27
28
@@ -35,7 +36,7 @@ val list: List[Any] = List(
35
36
)
36
37
37
38
list.foreach(element => println(element))
38
-
````
39
+
```
39
40
40
41
It defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, but they all are instance of `scala.Any`, so you can add them to the list.
41
42
@@ -48,3 +49,26 @@ c
48
49
true
49
50
<function>
50
51
```
52
+
## Type Casting
53
+
Value types can be cast in the following way:
54
+

55
+
For example:
56
+
```tut
57
+
val x: Long = 987654321
58
+
val y: Float = x // 9.8765434E8 (note that some precision is lost in this case)
59
+
60
+
val face: Char = '☺'
61
+
val number: Int = face // 9786
62
+
```
63
+
Casting is unidirectional. This will not compile:
64
+
```
65
+
val x: Long = 987654321
66
+
val y: Float = x // 9.8765434E8
67
+
val z: Long = y // Does not conform
68
+
```
69
+
You can also cast a reference type to a subtype. This will be covered later in the tour.
70
+
71
+
## Nothing and Null
72
+
`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).
73
+
74
+
`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.
0 commit comments