Search code examples
scalalanguage-featuresdelay

How would you define a delayed (by-name) parameter that accepts a function that takes parameters?


I saw a delayed example in David Pollak's "Beginning Scala". I tried to adapt that, by trial and error. Here's what I have:

def sayhello() = {
  println("hello")
}

def delaying(t: => Unit):Unit = {
  println("before call")
  t
  println("after call")
}

delaying(sayhello())

How would you delay a function/method that takes parameters? Why can't I use parantheses when I call t? Where can I find more documentation on delaying functions?


Solution

  • t does not have to be a function value. It is simply any passed-by-name value that evaluates to Unit.

    When you state t in the delaying function, you're not explicitly calling the function that was passed as that parameter. By stating it, you are forcing the passed-by-name parameter to be evaluated, which means sayhello() is evaluated at that point.

    There's no reason you can't use parameters in functions in passed-by-name parameters:

    def say(word: String) {
      println(word)
    }
    
    def delaying(t: => Unit) {
      println("before call")
      t
      println("after call")
    }
    
    delaying(say("hello"))
    

    The only time you would attach parameters to the t inside delaying would be if its return type was (not Unit but) a function type taking parameters.