Skip to content

Commit 97288f2

Browse files
committed
Rewrote unified types tour
1 parent bcef8df commit 97288f2

File tree

5 files changed

+274
-5
lines changed

5 files changed

+274
-5
lines changed

‎tutorials/tour/Makefile

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

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ next-page: classes
1111
previous-page: basics
1212
---
1313

14-
In Scala, all values are instances of a class, including numerical values and functions. The diagram below illustrates the class hierarchy.
14+
In Scala, all values are instances of a class, including numerical values and functions. The diagram below illustrates a subset the class hierarchy.
1515

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

1818
## Scala Class Hierarchy ##
1919

20-
The superclass of all classes `scala.Any` has two direct subclasses: `scala.AnyVal` and `scala.AnyRef`.
20+
`scala.Any` is the base type of all types. It defines certain universal methods: `==`, `!=`, `equals`, `hashCode`, `toString`. `scala.Any` has two direct subclasses: `scala.AnyVal` and `scala.AnyRef`.
2121

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.
22+
`scala.AnyVal` represents value classes. All value classes are non-nullable and predefined; they correspond to the primitive types of Java-like languages: `Byte`, `Short`, `Char`, `Int`, `Long`, `Float`, `Double`, and `Boolean`. Scala adds the `Unit` type which is similar to Java's `void`.
2323

2424
`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`.
2525

@@ -35,7 +35,7 @@ val list: List[Any] = List(
3535
)
3636
3737
list.foreach(element => println(element))
38-
````
38+
```
3939

4040
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.
4141

@@ -48,3 +48,15 @@ c
4848
true
4949
<function>
5050
```
51+
52+
## Nothing and Null
53+
`Nothing` is a subtype of all types. There is no value that has type `Nothing`. There are two uses: The first is to signal abnormal termination such as an exception (i.e. it doesn't return). The second type usage is an element type of empty collections (e.g. `Set[Nothing]`).
54+
55+
`Null` is a subtype of all subtypes of `AnyRef`. The class name is `Null` but the literal identifier is `null`. You can assign any reference type to null.
56+
```tut
57+
val name: String = null
58+
```
59+
However, you can not assign value types to null:
60+
```
61+
val number: Int = null // does not compile
62+
```

‎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 dot
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
digraph UnifiedTypes {
2+
rankdir="BT"
3+
AnyVal -> Any;
4+
"scala.AnyRef (java.lang.Object)" -> Any;
5+
6+
"scala.Double", "scala.Float", "scala.Long", "scala.Int", "scala.Unit", "scala.Boolean", "scala.Char" -> AnyVal;
7+
"scala.List", "scala.Seq", "scala.Iterable" -> "scala.AnyRef (java.lang.Object)"
8+
9+
"scala.Null" -> {"scala.List" "scala.Seq" "scala.Iterable"}
10+
"scala.Nothing" -> {"scala.Double", "scala.Float", "scala.Long", "scala.Int", "scala.Unit", "scala.Boolean", "scala.Char", "scala.Null"}
11+
12+
{rank = same; "scala.Null"; "scala.Nothing"}
13+
}
Lines changed: 241 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)