Skip to content

Commit d5975e4

Browse files
committed
Rewrote unified types tour
1 parent 6b9bd6e commit d5975e4

File tree

7 files changed

+434
-7
lines changed

7 files changed

+434
-7
lines changed

‎tutorials/tour/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
unified-types:
2+
dot -Tsvg unified-types-diagram.dot -o unified-types-diagram.svg
3+
type-casting:
4+
dot -Tsvg type-casting-diagram.dot -o type-casting-diagram.svg

‎tutorials/tour/_posts/2017-02-13-unified-types.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ categories: tour
99
num: 3
1010
next-page: classes
1111
previous-page: basics
12+
prerequisite-knowledge: classes, basics
1213
---
1314

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.
1516

16-
![Scala Type Hierarchy]({{ site.baseurl }}/resources/images/classhierarchy.img_assist_custom.png)
17+
![Scala Type Hierarchy]({{ site.baseurl }}/tutorial/tour/unified-types-diagram.svg)
1718

18-
## Scala Class Hierarchy ##
19+
## Scala Type Hierarchy ##
1920

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`.
2122

22-
`scala.AnyVal` represents value classes. All value classes are non-nullable and 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.
2324

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`.
2526

2627
Here is an example that demonstrates that strings, integers, characters, boolean values, and functions are all objects just like every other object:
2728

@@ -35,7 +36,7 @@ val list: List[Any] = List(
3536
)
3637
3738
list.foreach(element => println(element))
38-
````
39+
```
3940

4041
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.
4142

@@ -48,3 +49,26 @@ c
4849
true
4950
<function>
5051
```
52+
## Type Casting
53+
Value types can be cast in the following way:
54+
![Scala Type Hierarchy]({{ site.baseurl }}/tutorial/tour/type-casting-diagram.svg)
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.

‎tutorials/tour/dot-hot-reload.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ls *.dot | entr make $1 # Choose either unified-types or type-casting (see Makefile)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
digraph UnifiedTypes {
2+
node [fontname = "Courier"];
3+
rankdir="BT"
4+
5+
6+
Byte -> Short;
7+
Short -> Int;
8+
Int -> Long;
9+
Long -> Float;
10+
Float -> Double;
11+
Char -> Int;
12+
13+
{rank = same; Double; Float; Long; Int; Short; Byte; }
14+
}
Lines changed: 91 additions & 0 deletions
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
digraph UnifiedTypes {
2+
node [fontname = "Courier"];
3+
rankdir="BT"
4+
AnyVal -> Any;
5+
"AnyRef (java.lang.Object)" -> Any;
6+
7+
Double, Float, Long, Int, Short, Byte, Unit, Boolean, Char -> AnyVal;
8+
List, Option, YourClass -> "AnyRef (java.lang.Object)"
9+
10+
Null -> {List Option YourClass}
11+
"Nothing" -> {Double, Float, Long, Int, Short, Byte, Char, Unit, Boolean, Null}
12+
13+
{rank = min; "Nothing"}
14+
{rank = same; Double; Float; Long; Int; Short; Byte; Char; Unit; Boolean; List; Option; YourClass}
15+
{rank = same; "AnyRef (java.lang.Object)"; AnyVal}
16+
}

0 commit comments

Comments
 (0)