{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "Tour of Scala
\n", "\n", "\n", "# Unified Types\n", "\n", "In Scala, all values have a type, including numerical values and functions. The diagram below illustrates a subset of the type hierarchy.\n", "\n", "\n", "\n", "## Scala Type Hierarchy ##\n", "\n", "[`Any`](http://www.scala-lang.org/api/2.12.1/scala/Any.ipynb) 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`.\n", "\n", "`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.\n", "\n", "`AnyRef` represents reference types. All non-value types are defined as reference types. 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`.\n", "\n", "Here is an example that demonstrates that strings, integers, characters, boolean values, and functions are all objects just like every other object:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a string\n", "732\n", "c\n", "true\n", "ammonite.$sess.cmd0$Helper$$Lambda$2352/2102673157@2859fdff\n" ] }, { "data": { "text/plain": [ "\u001b[36mlist\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mAny\u001b[39m] = \u001b[33mList\u001b[39m(\n", " \u001b[32m\"a string\"\u001b[39m,\n", " \u001b[32m732\u001b[39m,\n", " \u001b[32m'c'\u001b[39m,\n", " true,\n", " ammonite.$sess.cmd0$Helper$$Lambda$2352/2102673157@2859fdff\n", ")" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val list: List[Any] = List(\n", " \"a string\",\n", " 732, // an integer\n", " 'c', // a character\n", " true, // a boolean value\n", " () => \"an anonymous function returning a string\"\n", ")\n", "\n", "list.foreach(element => println(element))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "Here is the output of the program:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "a string\n", "732\n", "c\n", "true\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 }