Packages in Scala
To all the namespace lovers out there, Scala has its own namespaces, called packages
.
Defining
Packages can be defined by declaring the package name on top of the file
package Shapesclass Triangle
Packages declared this way normally follow a file nesting convention.
Packages can also be declared in a single file by nesting package definitions inside one another.
package Shapes { package TwoD { class Triangle class Square } package ThreeD { class Cone class Cube } }
Packages can also be nested without having to nest the declarations.
// instead of package Shapes: package TwoD: class Triangle // do package Shapes.TwoD class Triangle
Importing
Importing packages are quite easy.
// Import just Triangle import Shapes.TwoD.Triangle // Import Triangle and Square import Shapes.TwoD.{ Triangle, Square } // Import all 2D shapes import Shapes.TwoD._ // Import all 2D shapes under a namespace import Shapes.TwoD._ as Shapes2d