Skip to content

Instantly share code, notes, and snippets.

@MarounMaroun
Created December 28, 2017 08:50
Show Gist options
  • Save MarounMaroun/0e2738bfa0a0923ddbe9d3d600ca5d92 to your computer and use it in GitHub Desktop.
Save MarounMaroun/0e2738bfa0a0923ddbe9d3d600ca5d92 to your computer and use it in GitHub Desktop.
Call by name VS call by value in Scala
// 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