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-inner-classes.md
+5-3Lines changed: 5 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,9 @@ next-page: abstract-types
11
11
previous-page: lower-type-bounds
12
12
---
13
13
14
-
In Scala it is possible to let classes have other classes as members. Opposed to Java-like languages where such inner classes are members of the enclosing class, in Scala such inner classes are bound to the outer object. To illustrate the difference, we quickly sketch the implementation of a graph datatype:
14
+
In Scala it is possible to let classes have other classes as members. Opposed to Java-like languages where such inner classes are members of the enclosing class, in Scala such inner classes are bound to the outer object. Suppose we want the compiler to prevent us, at compile time, from mixing up which nodes belong to what graph. Path-dependent types provide a solution.
15
+
16
+
To illustrate the difference, we quickly sketch the implementation of a graph datatype:
15
17
16
18
```tut
17
19
class Graph {
@@ -31,7 +33,7 @@ class Graph {
31
33
}
32
34
}
33
35
```
34
-
This program represents a graph as a list of nodes (`var nodes`). Each node knows which other nodes it's connected to (`connectedNodes`). The `class Node` is a _path-dependent type_ because it is nested in the `class Graph`. Therefore, everything in the `connectedNodes` must be created using the class attached to Graph.
36
+
This program represents a graph as a list of nodes (`List[Node]`). Each node has a list of other nodes it's connected to (`connectedNodes`). The `class Node` is a _path-dependent type_ because it is nested in the `class Graph`. Therefore, all noes in the `connectedNodes` must be created using the `newNode` from the same instance of `Graph`.
35
37
36
38
```tut
37
39
val graph1: Graph = new Graph
@@ -41,7 +43,7 @@ val node3: graph1.Node = graph1.newNode
41
43
node1.connectTo(node2)
42
44
node3.connectTo(node1)
43
45
```
44
-
Notice how the type of all of the nodes is`graph1.Node`. This is because when we call `graph1.newNode` which calls `new Node`, the method is using the instance of `Node` specific to the instance `graph1`.
46
+
We have explicitly declared the type of `node1`, `node2`, and `node3` as`graph1.Node` for clarity but the compiler could have inferred it. This is because when we call `graph1.newNode` which calls `new Node`, the method is using the instance of `Node` specific to the instance `graph1`.
45
47
46
48
If we now have two graphs, the type system of Scala does not allow us to mix nodes defined within one graph with the nodes of another graph, since the nodes of the other graph have a different type.
0 commit comments