liveBook · Manning

Sealed trait

A sealed trait is a Scala trait whose permitted subtypes are restricted to declarations known to the compiler.

How 2 Manning books explain it

Akka in Action, Second Edition

A sealed trait is a Scala trait whose permitted direct subtype cases are restricted so the compiler can check pattern-match exhaustiveness.

How it is used
Structures typed actor protocols so messages form an explicit, compiler-checkable hierarchy.
How deep it goes
Defines sealed traits and exhaustiveness warnings, then applies them in a wallet actor without detailing compiler internals.
Examples
Uses a wallet with Increase and Decrease commands, bounded state, and stopping after an excessive increase.
What you can do after
Build typed actor protocols whose pattern matches receive compiler warnings for omitted command cases.

Read how Akka in Action, Second Edition explains sealed trait →

Functional Programming in Scala, Second Edition

A sealed trait hierarchy is a Scala type hierarchy whose permitted subtypes are restricted to declarations known to the compiler.

How it is used
Defines closed algebraic data types whose constructors support reliable recursive operations and exhaustive pattern matching.
How deep it goes
Explains traits, same-file constructors, companion objects and list implementation, but defers minor trait differences and broader abstraction details.
Examples
Uses List with Cons and Nil, plus recursive size operations, to demonstrate a closed constructor set.
What you can do after
Build sealed Scala data types with case-based operations and compiler-checkable exhaustive matches.

Read how Functional Programming in Scala, Second Edition explains sealed trait →

What to know first

Related concepts

Questions about sealed trait

What is sealed trait?

A sealed trait is a Scala trait whose permitted subtypes are restricted to declarations known to the compiler.

Which Manning books explain sealed trait?

Akka in Action, Second Edition and Functional Programming in Scala, Second Edition.

What should I know before learning sealed trait?

Manning books that teach sealed trait usually introduce pattern matching first.

What is the difference between sealed trait and enum?

A sealed trait is a Scala trait whose permitted subtypes are restricted to declarations known to the compiler. An enum is a type whose values are selected from a finite set of named alternatives.

What is sealed trait a kind of?

Sealed trait is a kind of trait. A trait is a type-level construct that defines reusable behavior or members that types can implement or compose.

What is sealed trait part of?

Sealed trait is part of actor protocol. An actor protocol is the set of message types and communication rules that defines how an actor can be addressed.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.