Open In App

Scala | Sealed Trait

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 Program that illustrates sealed trait
// language.scala
sealed trait Geeks
{
    val article="not done"
}
 
// Class extends trait
class Scala extends Geeks
{
    override val article = "scala article"
}
 
// Class extends trait
class Java extends Geeks
{
    override val article = "java article"
}
 
// Class extends trait
class Csharp extends Geeks
{
    override val article = "csharp article"
}
 
// Creating object
object GFG
{
    // Main method
    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))
    }
     
    // Defined function
    def checkArticle(Article: Geeks): String = Article match
    {
        case s: Scala  => s.article
        case j: Java   => j.article
        case c: Csharp => c.article
        //exclusion of <strong>line 45</strong> would lead to warning
    }
}

Output : 

scala article
java article
csharp article
some important points
illegal inheritance from sealed trait bag
import geeks
class python extends geeks{
    val article="python article";
}




// Scala Program that illustrates sealed trait
// By using Enumeration
sealed trait card extends Enumeration
 
// Class extends trait
case object CLUB extends card
 
// Class extends trait
case object HEART extends card
 
// Class extends trait
case object DIAMOND extends card
 
// Class extends trait
case object SPADE extends card
 
// Creating object
object obj1
{  
    // Main method
    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))
    }
     
    // Defined function
    def checkcard(x: card): String = x match
    {
         
        case HEART   =>"heart"
        case CLUB    =>"club"
        case SPADE   =>"spade"
        case DIAMOND =>"diamond"
    }
}

Output : 

heart
club
spade
diamond

 


Article Tags :