Inside DrMark’s Lab

Inside DrMark’s Lab

Product with Serializable in Scala 3, Without the Ceremony

The Unshielded Mind's avatar
The Unshielded Mind
Sep 03, 2025
∙ Paid
Share

Scala’s algebraic data types often work best as a sum of case classes and case objects. Adding Product with Serializable at the root makes that intention precise, keeps type inference tidy in older ecosystems, and unlocks simple yet powerful tooling in plain Scala 3. The examples below that are in my Github repo are reworked for a clean Substack read. Case classes already implement both traits, the benefit comes from placing the constraint on the supertype so you can use the supertype safely with libraries and utilities that require serializable values. This idiom appears in community guidance, historical best practices, and write-ups that explain why type inference sometimes widens to Product with Serializable with X when the root does not extend them.

Case classes and tuples implement Product, and case classes also implement Serializable. If the sealed root does not, the compiler may infer types like List[Product with Serializable with Color] instead of List[Color] in some contexts. Extending the root with these traits lets the compiler pick the domain type cleanly and keeps generic code happy. Scala 3 improves inference and treats such traits as transparent in many flows, yet the root-level extension remains helpful for interop with frameworks that still require Serializable.

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 Markgrechanik@gmail.com
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture