Created
September 22, 2012 23:09
-
-
Save klgraham/3768168 to your computer and use it in GitHub Desktop.
Call-by-name vs call-by-value in Scala
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
/* call-by-value means the parameters are evaluated left to | |
** right to determine their value before the function itself | |
** is evaluated | |
*/ | |
def first(a: Int, b: Int): Int = a | |
first(3 + 4, 5 + 6) // will be reduced to first(7, 5 + 6), then first(7, 11), and then 7 | |
/* call-by-name means the paramter is passed into the function | |
** as is. Parameter evaluation takes place after | |
** substitution | |
*/ | |
def first1(a: Int, b: => Int): Int = a | |
first1(3 + 4, 5 + 6) // will be reduced to (3 + 4) and then to 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment