Scala | Method Invocation


Method Invocation is a technique which demonstrate different synatx in which we dynamically call methods of a class with an object.
The naming conventions of Scala are same as of Java, that are:-

  • There should not be any space between the invocation object/target and the dot(.) nor a space between the dot and method name.

    obj.display(“name”) // correct
    obj.display (“name”) // incorrect but legal
    obj. display(“name”) // incorrect but legal

  • Nor there should not be any space between the method name and the parantheses.

    println(“student”) // correct
    println (“student”) // incorrect but legal

  • The arguments should be seperated by a single space and a comma.

    obj.display(“name”, 22) // correct
    obj.display (“name”, 22) // incorrect but legal
    obj. display(“name”, 22) // incorrect but legal

Let’s see methods with different arguments and styles.
 
Arity-0 : When there are 0 arguments to be passed to the method. Hence it is not mandatory to add parenthesis on methods. It will enhance the readability of the code and omission of parenthesis will reduce the number of characters to some extent.

obj.display() //correct
obj.display   //correct

 
Arity-1 : When there is only 1 argument to be passed to the method of arity-1. This rule should be used for purely-functional programming or the methods that take functions as parameters, then it is parenthesis can be avoided around the passed parameter. This type of syntax is also known as infix notation.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program for arity 1 
// Creating object
class x
{
    // Defining method
    def display(str: String)= 
    {
        println(str)
    }
}
  
// Creating object
object GfG 
    // Main method 
    def main(args: Array[String]) 
    
        val obj = new x
        obj.display("student") // correct
        obj display ("student") // correct
        obj display "student" // correct
      
    

chevron_right


Output:

student
student
student

 
Higher order function : A function is called Higher Order Function if it contains other functions as a parameter or returns a function as an output i.e, the functions that operate with another functions are known as Higher order Functions. It is worth knowing that higher order function is applicable for functions and methods as well that takes functions as parameter or returns a function as a result. This is practicable as the compiler of Scala allows to force methods into functions.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of higher order 
// function 
  
// Creating object 
object GfG 
    // Main method 
    def main(args: Array[String]) 
    
      
        // Creating a Set of strings 
        val names = Set("sunil","akhil","rakhi"
      
        // Defining a method 
        def captainDesignation = (y: String) => "captain " + y
      
        // Creating a higher order function 
        // that is assigned to the String elements 
        val result = names.map(y => captainDesignation(y)) 
      
        // Displays output 
        println("Multiplied List is: " + result) 
    

chevron_right


Output:

Multiplied List is: Set(captain sunil, captain akhil, captain rakhi)

Important Points

  • These syntax should be followed when there are no side-effects.
  • These conventions are used to improve the readability and make the code much easier to understand.
  • It can save some space by neglecting some extra characters.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.