Skip to content

Instantly share code, notes, and snippets.

@nsfyn55
Created November 26, 2012 19:09
Show Gist options
  • Save nsfyn55/4149981 to your computer and use it in GitHub Desktop.
Save nsfyn55/4149981 to your computer and use it in GitHub Desktop.
By Name Parameter
/*
a by name parameter is not evaluated at the point of function application
but rather it is evaluated at each use within the function
*/
object App {
def main (args: Array[String]){
println(delayed(nano()))
}
def nano() ={
println("Getting Nano")
System.nanoTime
}
def delayed(t: => Long) = { // indicates by-name parameter
println("In Delayed Method")
println("Param: " + t) //evaluates t
t //Evaluates t again
//This results in Getting Nano being printed
//twice
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment