Friday 10 March 2017

Variables / Arguments evaluation rules in Scala


Evaluation Rules in Scala

Call by Value: Evaluates the function arguments before calling the function

Call by Name: Evaluates the function first and then evaluates the arguments



def example = 2 // evaluated when called
val example = 2 //evaluated immediately
lazy val example = 2 //evaluated once when needed
def square(x: Double) // call by value def square(x: => Double) // call by name def myFct(bindings: Int*) = { ... } // bindings is a sequence of int,
// containing a varying # of arguments

No comments:

Post a Comment