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
*/
Code by IncludeHelp, on August 13, 2022 21:13
Reference: alvinalexander.com

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.