Scala - The sealed trait of a big 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

Code compilation completed successfully.

Why can it be defined SealedGranchild

even if it is a closed ancestor type in another file? Aside from explicitly declaring SealedChild

how sealed

, is there a way to prevent the extension SealedChild

outside of the file where it is defined?

+3


source to share


1 answer


The modifier sealed

only applies to direct children sealed

, it does not apply to the entire inheritance tree. Spreading it out in all cases would be very restrictive, so you need to repeat the modifier for every level of the tree you want to seal.



+6


source







All Articles