Sealed provides exhaustive checking for our application. Exhaustive checking allows to check that all members of a sealed trait must be declared in the same file as of the source file. That means that all the possible known members of a trait that must be included are known by the compiler in advance. So this gives us advantage to prevent mistakes in our code.
Syntax :
sealed trait X
class A extends X
class B extends X
class C extends X
Exhaustive checking is mostly used in type / pattern matching in scala. Let’s say we have a sealed trait X and classes that extends trait X. On matching sub-types of trait X we have to make sure that we inclusion of all known sub-types is a must. Below method would give us a warning. Though we would get the correct output, but this may could be lead to unexpected runtime crashes in our application.
Warning: match may not be exhaustive
def obj(item: X) = item match {
case A => //
case B => //
}
Correct implementation would be like-
def obj(item: X) = item match{
case A => //
case B => //
case C => //
or
case _ => //for covering all the remaining cases
}
Let us take a view on below program in file saves as language.scala:-
Example :
Scala
sealed trait Geeks
{
val article="not done"
}
class Scala extends Geeks
{
override val article = "scala article"
}
class Java extends Geeks
{
override val article = "java article"
}
class Csharp extends Geeks
{
override val article = "csharp article"
}
object GFG
{
def main(args: Array[String])
{
val s = new Scala
val j = new Java
val c = new Csharp
println(checkArticle(s))
println(checkArticle(j))
println(checkArticle(c))
}
def checkArticle(Article: Geeks): String = Article match
{
case s: Scala => s.article
case j: Java => j.article
case c: Csharp => c.article
}
}
|
Output :
scala article
java article
csharp article
some important points
- Sub-types of a trait are known in advance- Not including any of the sub-type of sealed class C in pattern match would give us warning. Such a warning tells you that there’s a risk your code might produce a Match Error exception because some possible patterns are not handled. The warning points to a potential source of run-time faults, so it is usually a welcome help in getting your program right.
- Sealed traits can only extend in the same source file as of sub-types- In above example, we have another class python in another scala file. Importing the trait geeks from language.scala we would get error message as below.
illegal inheritance from sealed trait bag
import geeks
class python extends geeks{
val article="python article";
}
- Sealed class is also mostly used in enums– Preventing illegal inheritance and using all the sub-type so to avoid exhaustive matching warnings.
Example :
Scala
sealed trait card extends Enumeration
case object CLUB extends card
case object HEART extends card
case object DIAMOND extends card
case object SPADE extends card
object obj1
{
def main(args: Array[String])
{
val card1 = HEART
val card2 = CLUB
val card3 = SPADE
val card4 = DIAMOND
println(checkcard(card1))
println(checkcard(card2))
println(checkcard(card3))
println(checkcard(card4))
}
def checkcard(x: card): String = x match
{
case HEART =>"heart"
case CLUB =>"club"
case SPADE =>"spade"
case DIAMOND =>"diamond"
}
}
|
Output :
heart
club
spade
diamond
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
19 Oct, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...