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

Previous

\n", "

Next

\n", "

Tour of Scala

\n", "
\n", "\n", "# Packages and Imports\n", "\n", "# Packages and Imports\n", "Scala uses packages to create namespaces which allow you to modularize programs.\n", "\n", "## Creating a package\n", "Packages are created by declaring one or more package names at the top of a Scala file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```scala\n", "package users\n", "\n", "class User\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One convention is to name the package the same as the directory containing the Scala file. However, Scala is agnostic to file layout. The directory structure of an sbt project for `package users` might look like this:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "- ExampleProject\n", " - build.sbt\n", " - project\n", " - src\n", " - main\n", " - scala\n", " - users\n", " User.scala\n", " UserProfile.scala\n", " UserPreferences.scala\n", " - test\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice how the `users` directory is within the `scala` directory and how there are multiple Scala files within the package. Each Scala file in the package could have the same package declaration. The other way to declare packages is by using braces:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```scala\n", "package users {\n", " package administrators {\n", " class NormalUser\n", " }\n", " package normalusers {\n", " class NormalUser\n", " }\n", "}\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, this allows for package nesting and provides greater control for scope and encapsulation.\n", "\n", "The package name should be all lower case and if the code is being developed within an organization which has a website, it should be the following format convention: `..`. For example, if Google had a project called `SelfDrivingCar`, the package name would look like this:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```scala\n", "package com.google.selfdrivingcar.camera\n", "\n", "class Lens\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This could correspond to the following directory structure: `SelfDrivingCar/src/main/scala/com/google/selfdrivingcar/camera/Lens.scala`.\n", "\n", "## Imports\n", "`import` clauses are for accessing members (classes, traits, functions, etc.) in other packages. An `import` clause is not required for accessing members of the same package. Import clauses are selective:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```scala\n", "import users._ // import everything from the users package\n", "import users.User // import the class User\n", "import users.{User, UserPreferences} // Only imports selected members\n", "import users.{UserPreferences => UPrefs} // import and rename for convenience\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One way in which Scala is different from Java is that imports can be used anywhere:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mfunction\u001b[39m \u001b[36msqrtplus1\u001b[39m" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def sqrtplus1(x: Int) = {\n", " import scala.math.sqrt\n", " sqrt(x) + 1.0\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the event there is a naming conflict and you need to import something from the root of the project, prefix the package name with `_root_`:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```scala\n", "package accounts\n", "\n", "import _root_.users._\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: The `scala` and `java.lang` packages as well as `object Predef` are imported by default.\n", "

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 }