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)

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
// OR
person sayHello

Call By Value vs Call By Name

Call By Value is just like any other programming language where we use the static value of the argument directly

     def calledByValue(x: Long): Unit = {
         println("by value: " + x)
         println("by value: " + x)

Call By Name
Here instead of the value, the expression is passed as is and it will be evaluated by the compiler everytime. Call by Name is lazily evaluated.

     def calledByName(x: => Long): Unit = {
         // the arrow above is going to tell the compiler to evaluate the parameter by NAME
         // x is evaluated for every use
         println("by name: " + x)
         println("by name: " + x)

Lazy Evaluation example

def infinite(): Int = 1 + infinite()
def printFirst(x: Int, y: => Int) = println(x)

// println(infinite(), 34) // errors with stackoverflow

println(34, infinite()) // runs fine as second parameter is lazily evaluated and is never executed

Scala Type Hierarchy and Expressions

Type Hierarchy

In imperative languages like Java/Python we execute instructions. For example IF conditional statement is an Instruction. Whereas in Scala, we execute expressions.

IF conditional statements are expressions that can return some value.

val aConditionValue = if (aCondition) 5 else 3
Code Blocks are also expressions that can return something
val someOtherValue = {
        if (someValue) 233 else 422

Code Blocks are commonly used expression blocks which can contain one or more expressions and the return type is the return value from the last expression.

val aCodeBlock = {
   if(true) 54

Throwing exception from a method returns Nothing “()”
Any methods (println) that have side effects returns Unit
so on..

In functional programming languages writing loops are not encouraged. Whenever there is a need to write a loop we should go for recursion. Optimized way to write recursion is to use TAIL RECURSION technique.
To ensure we are using tail recursion we can use “@tailrec” before the function definition


Mostly technology with occasional sprinkling of other random thoughts


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


A topnotch site


Just another 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: