Scala's interpreter has two ways of parsing a function arguments: a call to a value (Call-by-value): The value of the parameter expression is evaluated first and then applied to the inside of the function; Bogwang Call (Call-by-name): Applies the non-evaluated argument expression directly to the inside of the function
Before entering the function, the value of the argument expression has been evaluated by the call method, and the Bogwang call is calculated from the value of the parameter expression inside the function.
This creates a phenomenon in which the interpreter evaluates the value of an expression once each time the Bogwang call is used. The delayed method, which uses the/= symbol in the variable name and variable type to set the Bogwang call.
Com.expgiga.ScalaTest
/**
*
*
/callbynametest {
main (args:array[string]): Unit = {
delayed ()
}
time () = {
println (" get", in nanoseconds ) )
system.nanotime ()
}
delayed (t: = = Long) = {//variable name and variable type use the-= symbol to set the Bogwang call
println ( "within delayed method ")
println (" Parameter:"+ t"
t
}
}
Execution Result:
Within the Delayed method
Get time in nanoseconds
Parameters: 24297258404397
Get time in nanoseconds