Skip to content

Instantly share code, notes, and snippets.

@piton4k
Created July 4, 2017 12:47
Show Gist options
  • Save piton4k/b41de296d76c8c9637df1c7145cffe53 to your computer and use it in GitHub Desktop.
Save piton4k/b41de296d76c8c9637df1c7145cffe53 to your computer and use it in GitHub Desktop.
def lazyFun(aThing: => Unit) = {
println("doing a thing")
aThing
println("done")
}
// correct
lazyFun(println("a thing"))
// incorrect - compiles thanks to value discard, but doesn't print
lazyFun(() => println("another thing"))
trait MyUnit
object MyUnit extends MyUnit
def lazyFun2(aThing: => MyUnit) = {
println("doing a thing")
aThing
println("done")
}
// correct
lazyFun2({ println("a thing"); MyUnit })
// these won't compile
// lazyFun2(() => { println("another thing"); MyUnit })
// lazyFun2({ () => println("another thing"); MyUnit })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment