Scala Method Notations
November 26, 2019 Leave a comment
Infix Notation/Operator Notation
Method with one parameter can be accessed with space (Natural Language)
class Person {
def sayHello(name: String) = println(s"Hello $name")
}
val person = new Person
person sayHello "Niranjan"
In scala we can also use +/-/*/#/$ as method names which makes it much more comfortable to code with unlike other languages. This is exactly what is happening in mathematical operators as they are also methods
println(1 + 3)
// ALL OPERATORS ARE METHODS in SCALA
println(1.+(3))
PREFIX NOTATION or Unary Operator
A Unary Operator is also a METHOD in SCALA
All UNARY Operators are methods with prefix “unary_”
val x = -1 // Its same as val y = 1.unary_-
POSTFIX Notation
Method with no parameter can be accessed without paranthesis
class Person {
def sayHello = println("Hello")
}
val person = new Person
person.sayHello
// OR
person sayHello
Recent Comments