Sealed classes are described in 'Programming in Scala', but sealed traits are not. Where can I find more information about a sealed trait?
I would like to know, if a sealed trait is the same as a sealed class? Or, if not, what are the differences? When is it a good idea to use a sealed trait (and when not)?
Definition. The sealed is a Scala keyword used to control the places where given trait or class can be extended. More concretely, the subclasses and the implementations can be defined only in the same source file as the sealed trait or class.
In scala, trait is a collection of abstract and non-abstract methods. You can create trait that can have all abstract methods or some abstract and some non-abstract methods. A variable that is declared either by using val or var keyword in a trait get internally implemented in the class that implements the trait.
Scala allows using the sealed modifier for traits, classes, and abstract classes. In the context of sealed, a class, trait, or abstract class functions the same except for the normal differences between classes and traits — for instance, an abstract class can receive parameters, and traits cannot.
Sealed classes are used to restrict the users from inheriting the class. A class can be sealed by using the sealed keyword. The keyword tells the compiler that the class is sealed, and therefore, cannot be extended. No class can be derived from a sealed class.
A sealed
trait can be extended only in the same file as its declaration.
They are often used to provide an alternative to enums
. Since they can be only extended in a single file, the compiler knows every possible subtypes and can reason about it.
For instance with the declaration:
sealed trait Answer case object Yes extends Answer case object No extends Answer
The compiler will emit a warning if a match is not exhaustive:
scala> val x: Answer = Yes x: Answer = Yes scala> x match { | case No => println("No") | } <console>:12: warning: match is not exhaustive! missing combination Yes
So you should use sealed traits (or sealed abstract class) if the number of possible subtypes is finite and known in advance. For more examples you can have a look at list and option implementations.
a sealed trait is the same as a sealed class ?
As far as sealed
goes, yes. They share the normal differences between trait
and class
, of course.
Or, if not, what are the differences ?
Moot.
When is it a good idea to use a sealed trait (and when not) ?
If you have a sealed class X
, then you have to check for X
as well as any subclasses. The same is not true of sealed abstract class X
or sealed trait X
. So you could do sealed abstract class X
, but that's way more verbose than just trait
and for little advantage.
The main advantage of using an abstract class
over a trait
is that it can receive parameters. That advantage is particularly relevant when using type classes. Let's say you want to build a sorted tree, for instance. You can write this:
sealed abstract class Tree[T : Ordering]
but you cannot do this:
sealed trait Tree[T : Ordering]
since context bounds (and view bounds) are implemented with implicit parameters. Given that traits can't receive parameters, you can't do that.
Personally, I prefer sealed trait
and use it unless some particular reason makes me use a sealed abstract class
. And I'm not talking about subtle reasons, but in-your-face reasons you cannot ignore, such as using type classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With