Open In App

Scala map isDefinedAt() method with example

The method isDefinedAt() is an abstract value member of trait PartialFunction, which is identical to the method contains and is observed in all the classes of PartialFunction. It inspects whether a value is contained in the function’s domain or not.

Example :




// Scala program of isDefinedAt()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating partial function
        val par : PartialFunction[Int,Int] =
        
            case 1 => 1
        }
          
        // Applying isDefinedAt() method
        val result = par.isDefinedAt(1)
          
        // Displays output
        println(result)
  
    }
}                                         
                  

Output:
true

Here, the value i.e, x defined above is present in the function domain so, it returns true.
Example :




// Scala program of isDefinedAt()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a map
        val map: Map[Int,Int] = Map(2 -> 3)
          
        // Applying isDefinedAt() method
        val result = map.isDefinedAt(5)
          
        // Displays output
        println(result)
  
    }
}     

Output:

false

Here, the value i.e, x defined above is not present in the function domain so, it returns false.


Article Tags :