Argument evaluation
When we call a method (or a function), we could ask for delayed argument evaluation. Following is an example Scala snippet:
scala> def aMethod(x: String) = | println(x) aMethod: (x: String)Unit scala> aMethod("Hello world") // call site Hello world
The method argument is evaluated at the call site. However, recall that functions are first class values. This allows us to write the method as follows:
scala> def aMethod_1(x: () => String) = | println(x()) // argument evaluated aMethod_1: (x: () => String)Unit scala> aMethod_1(() => "Hello world") // unevaluated argument Hello world
When aMethod_1
is called, the string argument is not evaluated at the call site. Inside the method definition, the function is called and the argument ...
Get Learning Functional Data Structures and Algorithms now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.