Home » 
        Code Examples » 
        Scala Code Examples
    
        
    Scala - A simple call-by-name method Code Example
    
    
        
    The code for A simple call-by-name method
object Test extends App {
  def time() = {
      println("Entered time() ...")
      System.nanoTime
  }
  // `t` is now defined as a by-name parameter
  def exec(t: => Long) = {
      println("Entered exec, calling t ...")
      println("t = " + t)
      println("Calling t again ...")
      t
  }
  println(exec(time()))
}
/*
Output:
Entered exec, calling t ...
Entered time() ...
t = 4329467093667204
Calling t again ...
Entered time() ...
4329467093744708
*/