Scala has concise syntax. Period. This is a huge plus point because of brevity and expressiveness in code and a minus point because it’s not too easy to the naked eyes. Let’s discuss the syntactic sugar in use of methods in this post.
Say you have a Calculator class with add() method as shown here.
class Calculator { def add(num1:Int,num2:Int):Int = { num1 + num2 } }
The add() method accepts two arguments and returns their sum. In Scala, we know that the last line of a method is taken as a return value, so return keyword is omitted. We can write the add() method like this as well. You can get rid of the curly braces completely.
class Calculator { def add(num1:Int,num2:Int):Int = num1 + num2 }
We can write a method and ignore the return type too like this.
class Calculator { def add(num1:Int,num2:Int) = num1 + num2 }
For the naked eyes, it may hurt initially, but it’s pretty easy when you get used to it.