, , . , - .
Scala:
- "call-by-name" - Java .
- "call-by-value" - Scala . , Single Abstract Method (SAM) .
"call-by-name" , , , .
, myFunction myFunction() .
"call-by-value" , , :
- - myFunction - , .
- - myFunction() - () .
:
def myFunction = 5 * 5
. , . myFunction() . : myFunction
def myFunction() = 5 * 5
. - . myFunction myFunction() - .
def myFunction = () => 5 * 5
( def val). - myFunction() - myFunction - val, .
, :
def enclosingFunc(myFunction: => Void)
. - myFunction. - myFunction() - .
includeingFunc :
enclosingFunc(n = 1; n +=1)
def enclosingFunc(myFunction: () => Int)
This is a call by value function . It is important how we call it in the aggregate of the closing function. If you call it without parentheses - myFunction - it will NOT be executed, but we just refer to the object that blocks it. If you call it using brackets - myFunction () - it will be called and executed.
In this case, includeingFunc can only be called as follows:
enclosingFunc( () => n = 1; n +=1)
source
share