Defining a closure is straightforward as it derives from the way a function can be defined:
let adder = |a, b| { return a + b }
At runtime, a closure is an instance of java.lang.invoke.MethodHandle
. This means that you can do
all the operations that method handles support, such as invoking them or inserting arguments as
illustrated in the following example:
let adder = |a, b| { return a + b } println(adder: invokeWithArguments(1, 2)) let addToTen = adder: bindTo(10) println(addToTen: invokeWithArguments(2))
As one would expect, this prints 3
and 12
.