Created
December 28, 2017 08:50
-
-
Save MarounMaroun/0e2738bfa0a0923ddbe9d3d600ca5d92 to your computer and use it in GitHub Desktop.
Call by name VS call by value in Scala
This file contains 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
// defining a simple function that returns the current time | |
def getTime = System.currentTimeMillis | |
// defining a function, by NAME, that prints two times: now and one second later | |
def getTimeByName(f: => Long) = { println(f); Thread.sleep(1000); println(f)} | |
// defining it again, but this time, by VALUE | |
def getTimeByValue(f: Long) = { println(f); Thread.sleep(1000); println(f)} | |
getTimeByName(getTime) | |
// prints: | |
// 1514451008323 | |
// 1514451009325 | |
getTimeByValue(getTime) | |
// prints: | |
// 1514451024846 | |
// 1514451024846 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment