The answer of ka4ell is correct, but does not mention why you would use () => Long
instead of just using Long
: with () => Long
or L
the execution of the calculation which returns the Long
is delayed. We only execute the functions at the moment we want the actual result, this is called lazy evaluation.
In your case:
case "*" =>
// Return a function which takes two Ls : u1 and u2
// and which returns an L : () => u1() * u2()
(u1:L, u2:L) => () => u1() * u2()
Let's define a simple function which returns an L
:
// return a function which will return a Long
// this could potentially be a very expensive calculation
def giveMeAnL(n: Long) = () => {
println("giveMeAnL is called")
n
}
If we would use an analogous function than the one in the case:
// analogous case "*" => ...
def multiply(u1:L, u2:L) = () => {
println("multiply is called")
u1() * u2()
}
// create two Ls
val (l1, l2) = (giveMeAnL(5L), giveMeAnL(2L))
val productL = multiply(l1, l2) // productL is of type L
The value productL
now contains an L
which can calculate the product of the two values. At this point, neither the product nor the two values are calculated. If we call the productL
value, the two values are calculated and the product of these values is calculated.
val product = productL()
// multiply is called
// giveMeAnL is called
// giveMeAnL is called
// product: Long = 10
If somewhere in your parsing steps, you want to ignore some L
values, the results of these L
s are never calculated, which improves the performance.
case "multiply first with 5" =>
(u1:L, u2:L) => () => u1() * 5L // u2 is never executed