Created
November 26, 2012 19:09
-
-
Save nsfyn55/4149981 to your computer and use it in GitHub Desktop.
By Name Parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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