Packages, Imports, Access Modifiers & Exports
Imports can appear anywhere (scoped to the enclosing block).
20.1 Packages
package com.example.app
// or nested/chained
package com.example
package app
// or block style
package com.example:
package util:
def helper() = ???20.2 Imports
Imports can appear anywhere (scoped to the enclosing block). Default imports: java.lang.*, scala.*, scala.Predef.*.
import scala.collection.mutable // import a package
import scala.collection.mutable.ArrayBuffer
import scala.math.{sqrt, Pi} // selective
import java.util.{List as JList, *} // rename + wildcard (Scala 3)
import scala.util.{Random as _, *} // hide Random
import cats.syntax.all.* // wildcard (Scala 2: _)
import given_ExecutionContext // explicit given import
import mymodule.given // all givens20.3 Access modifiers
everyone (public by default) the class and its companion only this instance (Scala 2; inferred automatically in 3) everything inside package pkg subclasses + package
20.4 Export clauses (Scala 3)
Delegate members without inheritance:
class Printer:
def print(s: String) = println(s)
def status = "ready"
class Office:
private val printer = Printer()
export printer.{print, status} // Office now has print and status
Office().print("hi")