common code

How to put common code in Scala package objects

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 6.7, “How to put common code in Scala package objects.”

Problem

You want to make functions, fields, and other code available at a package level, without requiring a class or object.

Solution

Put the code you want to make available to all classes within a package in a package object.