{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\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: `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 }