Scala - Chained Package


Scala provides chained packages for organizing code through packages. Chained packages are used to declare nested packages without using braces. So, you can write clean code.

Creating a Package

You can declare your packages in Scala at the top of a Scala file using the package keyword. These packages group related classes, traits, and objects together. So, you can organize code and control scope.

Basic Package Declaration

You can declare simple package in Scala like this -

package users

class User

By convention, package names should be all lowercase. Directory structure should mirror the package hierarchy. For example, this is directory structure for the above package -

- ExampleProject
  - build.sbt
  - project
  - src
    - main
      - scala
        - users
          - User.scala
          - UserProfile.scala
          - UserPreferences.scala
    - test

So your code is organized. You can also group related classes together. Hence this is easy to manage and understand.

Chained Packages

You can declare multiple nested packages using a single line for each level of the package hierarchy.

Using Chained Packages

You can use sequence of package in chained packages to declare and create nested package structure without braces. For example -

package com.tutorialspoint.scala.packageimport

class ExampleClass {
  def display(): String = "This is an example class in a chained package."
}

This is clean and easy to define nested packages for more code readability.

Package Naming Conventions

You should use the organization domain name in reverse as part of the package name. It organizes packages and keeps a clean directory structure. For example -

package com.tutorialspoint.selfdrivingcar.camera

class Lens

This corresponds to the following directory structure:

- SelfDrivingCar
  - src
    - main
      - scala
        - com
          - tutorialspoint
            - selfdrivingcar
              - camera
                - Lens.scala

It also avoids conflicts with packages from other organizations.

Importing in Chained Packages

Import statements bring required classes, traits, objects, and methods into scope. You can import classes from chained packages just like you do with flat packages.

Importing from Chained Packages

You can import a single class, multiple classes, and even all classes from a chained package. For example -

import com.tutorialspoint.scala.packageimport.ExampleClass
import com.tutorialspoint.scala.packageimport._

Hence you can control which parts of a package are brought into scope and avoid namespace pollution.

Example of Using Chained Packages

Following is the example which shows you how to create and usage of chained packages -

Directory Structure

- ExampleProject
  - src
    - main
      - scala
        - com
          - tutorialspoint
            - scala
              - packageimport
                - ExampleClass.scala
                - AnotherClass.scala
              - Main.scala

ExampleClass.scala

package com.tutorialspoint.scala.packageimport

class ExampleClass {
  def display(): String = "This is an example class in a chained package."
}

This class has method to display a message.

AnotherClass.scala

package com.tutorialspoint.scala.packageimport

class AnotherClass {
  def show(): String = "This is another class in the same chained package."
}

This class has method to display a different message.

Main.scala

package com.tutorialspoint.scala.packageimport

object Main {
  def main(args: Array[String]): Unit = {
    val example = new ExampleClass()
    val another = new AnotherClass()

    println(example.display())
    println(another.show())
  }
}

Following is the above example which shows you how to use the classes within the chained package. It creates instances of ExampleClass and AnotherClass and calls their respective methods.

Command

Save the above programs in their respective files. The following commands are used to compile and execute the program.

> scalac com/tutorialspoint/scala/packageimport/*.scala
> scala com.tutorialspoint.scala.packageimport.Main

Output

The output is -

This will produce the following result:
This is an example class in a chained package.
This is another class in the same chained package.

Chained Package Summary

  • Scala uses chained packages to create namespaces. It modularizes code and prevents naming conflicts.
  • Chained packages used to declare nested packages without using braces.
  • Import statements can bring required classes, traits, objects, and methods from chained packages into scope.
  • Chained packages provide greater control over scope and encapsulation. So, your code can be more modular and easier to maintain.