{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Previous

\n", "

Next

\n", "

Tour of Scala

\n", "
\n", "\n", "# Inner Classes\n", "\n", "In Scala it is possible to let classes have other classes as members. As 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.\n", "\n", "To illustrate the difference, we quickly sketch the implementation of a graph datatype:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mclass\u001b[39m \u001b[36mGraph\u001b[39m" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Graph {\n", " class Node {\n", " var connectedNodes: List[Node] = Nil\n", " def connectTo(node: Node) {\n", " if (connectedNodes.find(node.equals).isEmpty) {\n", " connectedNodes = node :: connectedNodes\n", " }\n", " }\n", " }\n", " var nodes: List[Node] = Nil\n", " def newNode: Node = {\n", " val res = new Node\n", " nodes = res :: nodes\n", " res\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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 nodes in the `connectedNodes` must be created using the `newNode` from the same instance of `Graph`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "data": { "text/plain": [ "\u001b[36mgraph1\u001b[39m: \u001b[32mGraph\u001b[39m = ammonite.$sess.cmd0$Helper$Graph@7b207b36\n", "\u001b[36mnode1\u001b[39m: \u001b[32mgraph1\u001b[39m.\u001b[32mNode\u001b[39m = ammonite.$sess.cmd0$Helper$Graph$Node@5a642e3f\n", "\u001b[36mnode2\u001b[39m: \u001b[32mgraph1\u001b[39m.\u001b[32mNode\u001b[39m = ammonite.$sess.cmd0$Helper$Graph$Node@69521f66\n", "\u001b[36mnode3\u001b[39m: \u001b[32mgraph1\u001b[39m.\u001b[32mNode\u001b[39m = ammonite.$sess.cmd0$Helper$Graph$Node@3e227bfd" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val graph1: Graph = new Graph\n", "val node1: graph1.Node = graph1.newNode\n", "val node2: graph1.Node = graph1.newNode\n", "val node3: graph1.Node = graph1.newNode\n", "node1.connectTo(node2)\n", "node3.connectTo(node1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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`.\n", "\n", "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.\n", "Here is an illegal program:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "cmd2.sc:7: type mismatch;\n", " found : Helper.this.graph2.Node\n", " required: Helper.this.graph1.Node\n", "val res2_6 = node1.connectTo(node3) // illegal!\n", " ^Compilation Failed" ] }, { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "Compilation Failed" ] } ], "source": [ "val graph1: Graph = new Graph\n", "val node1: graph1.Node = graph1.newNode\n", "val node2: graph1.Node = graph1.newNode\n", "node1.connectTo(node2) // legal\n", "val graph2: Graph = new Graph\n", "val node3: graph2.Node = graph2.newNode\n", "node1.connectTo(node3) // illegal!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The type `graph1.Node` is distinct from the type `graph2.Node`. In Java, the last line in the previous example program would have been correct. For nodes of both graphs, Java would assign the same type `Graph.Node`; i.e. `Node` is prefixed with class `Graph`. In Scala such a type can be expressed as well, it is written `Graph#Node`. If we want to be able to connect nodes of different graphs, we have to change the definition of our initial graph implementation in the following way:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mclass\u001b[39m \u001b[36mGraph\u001b[39m" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Graph {\n", " class Node {\n", " var connectedNodes: List[Graph#Node] = Nil\n", " def connectTo(node: Graph#Node) {\n", " if (connectedNodes.find(node.equals).isEmpty) {\n", " connectedNodes = node :: connectedNodes\n", " }\n", " }\n", " }\n", " var nodes: List[Node] = Nil\n", " def newNode: Node = {\n", " val res = new Node\n", " nodes = res :: nodes\n", " res\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Previous

\n", "

Next

\n", "

Tour of Scala

\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Scala (2.13)", "language": "scala", "name": "scala213" }, "language_info": { "codemirror_mode": "text/x-scala", "file_extension": ".scala", "mimetype": "text/x-scala", "name": "scala", "nbconvert_exporter": "script", "version": "2.13.1" } }, "nbformat": 4, "nbformat_minor": 4 }