Using Extractors with Pattern Matching In Scala Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Scala Extractor is defined as an object which has a method named unapply as one of its part. Extractors can be utilized in Pattern Matching. The unapply method will be executed spontaneously, While comparing the Object of an Extractor in Pattern Matching. Below is the example of Extractor with pattern matching. Example #1: Scala // Scala program of extractors // with pattern matching // Creating object object GfG { // Main method def main(args: Array[String]) { // Assigning value to the // object val x = GfG(25) // Displays output of the // Apply method println(x) // Applying pattern matching x match { // unapply method is called case GfG(y) => println("The value is: "+y) case _ => println("Can't be evaluated") } } // Defining apply method def apply(x: Double) = x / 5 // Defining unapply method def unapply(z: Double): Option[Double] = if (z % 5 == 0) { Some(z/5) } else None } Output: 5.0 The value is: 1.0 In above example, object name is GFG also we are using unapply method and applying case class with match expression. Example #2: Scala // Scala program of extractors // with pattern matching // Creating object object GFG { // Main method def main(args: Array[String]) { val x = GFG(15) println(x) x match { case GFG(num) => println(x + " is bigger two times than " + num) // Unapply is invoked case _ => println("not calculated") } } def apply(x: Int) = x * 2 def unapply(z: Int): Option[Int] = if (z % 2 == 0) Some(z/2) else None } Output: 30 30 is bigger two times than 15 When comparing an extractor object using the match statement the unapply method will be automatically executed. Note: A Case class already has an Extractor in it so, it can be utilized spontaneously with Pattern Matching. Comment More info D DivyaPareek Follow Improve Article Tags : Scala Explore OverviewScala Programming Language3 min readIntroduction to Scala7 min readSetting up the environment in Scala3 min readHello World in Scala2 min readBasicsScala Keywords2 min readScala Identifiers3 min readData Types in Scala3 min readVariables in Scala3 min readControl StatementsScala | Decision Making (if, if-else, Nested if-else, if-else if)5 min readScala | Loops(while, do..while, for, nested loops)5 min readBreak statement in Scala3 min readScala | Literals4 min readOOP ConceptsClass and Object in Scala5 min readInheritance in Scala5 min readOperators in Scala11 min readScala Singleton and Companion Objects3 min readScala Constructors4 min readScala | Polymorphism5 min readScala | Multithreading3 min readScala this keyword2 min readMethodsScala | Functions - Basics3 min readAnonymous Functions in Scala2 min readScala | Closures3 min readRecursion in Scala4 min readMethod Overloading in Scala5 min readMethod Overriding in Scala8 min readLambda Expression in Scala4 min readScala Varargs2 min readStringsScala String4 min readScala | String Interpolation3 min readScala | StringContext2 min readRegular Expressions in Scala5 min readStringBuilder in Scala4 min readScala PackagesPackages In Scala4 min readScala | Package Objects3 min readChained Package Clauses in Scala3 min readFile Handling in Scala3 min readScala TraitScala | Traits7 min readScala | Sealed Trait4 min readScala | Trait Mixins3 min readTrait Linearization in Scala5 min readCollectionsScala Lists5 min readScala ListBuffer6 min readListSet in Scala6 min readScala Map5 min readScala | Arrays6 min readScala | ArrayBuffer4 min readScala | Tuple5 min readSet in Scala | Set-13 min readSet in Scala | Set-27 min readBitSet in Scala5 min readHashSet In Scala4 min readStack in Scala3 min readHashMap in Scala3 min readTreeSet in Scala4 min readIterators in Scala5 min readScala | Option3 min read Like