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
// 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
- 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
// 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 obj 1 { // Main method def main(args : Array[String]) { val card 1 = HEART val card 2 = CLUB val card 3 = SPADE val card 4 = DIAMOND println(checkcard(card 1 )) println(checkcard(card 2 )) println(checkcard(card 3 )) println(checkcard(card 4 )) } // 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
Please Login to comment...