76

What is the difference between sealed abstract and abstract Scala class?

2 Answers 2

96

The difference is that all subclasses of a sealed class (whether it's abstract or not) must be in the same file as the sealed class.

2
  • 38
    Something not so obvious (at least it was not for me :-)) is that "grand children" of the sealed class can be in other files too: Given sealed class A; B extends A; C extends B. B must be in the same file as A, but C can leave in the same or in another. Jun 14, 2010 at 6:49
  • 9
    @SandorMurakozi You must declare B as a sealed class as well, if you want to achieve that. Sealing only deals with direct inheritance.
    – natbusa
    Oct 10, 2014 at 9:51
80

As answered, all directly inheriting subclasses of a sealed class (abstract or not) must be in the same file. A practical consequence of this is that the compiler can warn if the pattern match is incomplete. For instance:

sealed abstract class Tree
case class Node(left: Tree, right: Tree) extends Tree
case class Leaf[T](value: T) extends Tree
case object Empty extends Tree

def dps(t: Tree): Unit = t match {
  case Node(left, right) => dps(left); dps(right)
  case Leaf(x) => println("Leaf "+x)
  // case Empty => println("Empty") // Compiler warns here
}

If the Tree is sealed, then the compiler warns unless that last line is uncommented.

5
  • 1
    Why can't the compiler infer that pattern match is incomplete if there is no sealed keyword? Apr 8, 2014 at 12:06
  • 7
    @sasha.sochka Suppose I compile it and put it in a jar file, without the sealed keyword. It's all been compiled at that point, including the match statement. Now, another user grabs this jar and extends Tree. There's nothing preventing him from doing so, but, at that point, the match statement is no longer complete. Since he isn't compiling it, just using it from your jar, the compiler can't warn him. And since you did not know of it when you created the jar, it couldn't have warned you. Apr 8, 2014 at 17:32
  • @DanielCSobral, You wrote "but, at that point, the match statement is no longer complete". Isn't the match statement incomplete at this moment, when you compile the original code you posted (but without sealed keyword), before making a jar file? It looks like easy to infer, because even without new children (compiler doesn't know about them yet) there is no branch for Empty. And I'm talking about a warning for the person who creates a jar, not about a person who uses it. Apr 8, 2014 at 17:39
  • 1
    @sasha.sochka Well, I suppose it could warn that Empty is missing, but the point is that, even if you add Empty, it could still be incomplete, or not, depending on what happens at separate compilations. Apr 8, 2014 at 17:43
  • Yes, then I agree with you. Apr 8, 2014 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.