8.16. Scala function call-by-name

发布时间 : 2023-11-14 23:00:02 UTC      

Page Views: Stats unavailable

Scala’s interpreter has two ways to parse function parameters (function arguments):

  • call-by-value: first evaluate the value of the parameter expression, and then apply it inside the function

  • Ncall-by-name: applies unevaluated parameter expressions directlyto the inside of a function

Before entering the interior of the function, the value transfer method has already calculated the value of the parameter expression, while the alias call calculates the value of the parameter expression inside the function.

This creates the phenomenon that the interpreter evaluates the value of the expression each time a name call is used.

object Test {
   def main(args: Array[String]) {
        delayed(time());
   }

   def time() = {
      println("Obtain time in nanoseconds")
      System.nanoTime
   }
   def delayed( t: => Long ) = {
      println("Within the delayed method")
      println("parameter: " + t)
      t
   }
}

In the above example, we declared delayed method, which is used in variable names and variable types => symbol to set the alias call. Execute the above code, and the output is as follows:

$ scalac Test.scala
$ scala Test
Within the delayed method
Obtain time in nanoseconds
parameter: 241550840475831
Obtain time in nanoseconds

In the instance delay method prints a message indicating that it entersthe method, and then delay method prints the received value and finally returns t.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.