Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a sealed trait?

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)?

like image 739
John Threepwood Avatar asked Jun 26 '12 08:06

John Threepwood


People also ask

What is the use of sealed trait in Scala?

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.

What are Scala traits?

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.

What is sealed abstract class in Scala?

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.

What is true in the case of a sealed class?

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.


2 Answers

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.

like image 113
paradigmatic Avatar answered Sep 30 '22 22:09

paradigmatic


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.

like image 44
Daniel C. Sobral Avatar answered Sep 30 '22 22:09

Daniel C. Sobral