azelo
azelo

Reputation: 129

Scala - Sealed trait grand child

While experimenting with sealed traits, I found (to my surprise) that if in one file, I have this code

sealed trait Sealed
trait SealedChild extends Sealed

And in another file, I have

trait SealedGrandchild extends SealedChild

Compiling the code succeeds.

Why is it that SealedGranchild can be defined even though it has a sealed ancestor type in another file? Other than explicitly declaring SealedChild as sealed, is there a way to prevent the SealedChild from being extended outside the file where it's defined?

Upvotes: 4

Views: 223

Answers (1)

francoisr
francoisr

Reputation: 4595

The sealed modifier only applies to the direct children of Sealed, it is not propagated to the whole inheritance tree. Propagating it in alll cases would be very restrictive, so you have to repeat the modifier for each level of the tree you want sealed.

Upvotes: 6

Related Questions