Scala Method Notations

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Mawazo

Mostly technology with occasional sprinkling of other random thoughts

amintabar

Amir Amintabar's personal page

101 Books

Reading my way through Time Magazine's 100 Greatest Novels since 1923 (plus Ulysses)

Seek, Plunnge and more...

My words, my world...

ARRM Foundation

Do not wait for leaders; do it alone, person to person - Mother Teresa

Executive Management

An unexamined life is not worth living – Socrates

javaproffesionals

A topnotch WordPress.com site

thehandwritinganalyst

Just another WordPress.com site

coding algorithms

"An approximate answer to the right problem is worth a good deal more than an exact answer to an approximate problem." -- John Tukey

%d bloggers like this: