Method Invocation in Scala

Reading Time: 2 minutes

Introduction:

Method Invocation is a technique that shows various syntax in which, we precisely call methods of a class with an object.

The naming conventions of Scala are the same as of Java, that is:-

  • There should not be any space between the invocation object or target and the dot(.) nor a space between the dot and method name.
obj.present(“walk”) // correct
obj.present (“talk”) // incorrect but legal
obj. present(“hear”) // incorrect but legal
  • Nor there should not be any space between the method name and the parentheses.
println(“child1”) // correct
println (“child2”) // incorrect but legal
  • The arguments should be separated by a single space and a comma
obj.present(“student”, 22) // correct
obj.present (“student”, 22) // incorrect but legal
obj. present(“student”, 22) // incorrect but legal

Let’s see methods with different arguments and styles.

Arity-0 : 

When there are 0 contentions to be passed to the technique. Thus it isn’t required to include enclosure techniques. It will upgrade the meaningfulness of the code and oversight of enclosure will diminish the number of characters somewhat.

obj.present() //correct
obj.present   //correct

Arity-1 :

Scala has exceptional accentuation-free punctuation for summoning techniques for arity-1 (one contention). This ought to for the most part be kept away from, however with the accompanying exemptions for administrators and higher-request capacities. In these cases, it ought to just be utilized for simply utilitarian strategies (techniques with no aftereffects).

In other words, when there is simply 1 contention to be passed to the technique for arity-1. This standard ought to be utilized for absolutely useful programming or the techniques that accept capacities as boundaries, then, at that point, it is bracket can be stayed away from around the passed boundary. This sort of linguistic structure is likewise known as infix documentation.

Look at the example-

class example
{

    def present(str: String)= 
    {
        println(str)
    }
}
  

object GfG 
{ 

    def main(args: Array[String]) 
    { 
        val obj = new example
        obj.present("father") // correct
        obj present ("mother") // correct
        obj present "child" // correct
      
    } 
} 

The output will be –

father
mother
child

Conclusion:

This topic will be used in plenty of useful places in your projects. So, I would say, keep practicing. That’s all basic I have in my mind as of now, related to the topic, which could be understood by any beginner-level programmer. The language does not matter. You can write in any language you want to write. This 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.

So, focus on the logic. If you want to add anything or you do not relate to my view on any point, drop me a comment. I will be happy to discuss. For more blogs, click here

Written by 

Rituraj Khare is a Software Consultant at Knoldus Software LLP. An avid Scala programmer and Big Data engineer, he has experience with the tech stack such as - Scala| Spark| Kafka| Python| Unit testing| Git| Jenkins| Grafana.