Java Reference
In-Depth Information
Bound Functions
Our first brush with bind and JavaFX functions demonstrated how to bind to an
ordinary function. Bound functions and ordinary functions are similar in that
both, when part of a bind expression, will be recalculated if their arguments
change. However, they differ in one key area, namely how to interpret changes
inside the function body. Binding to ordinary functions treats the function body
as a black box. An internal change to the function body will not cause the func-
tion to be re-invoked. A bound function on the other hand will see changes both
at the argument level and inside the function causing it to get re-invoked. So let's
see how this plays out with a concrete example.
class Cell {
public var row : Integer;
public var col : Integer;
}
var translate = 0;
function moveToUnBound(r : Integer, c : Integer) : Cell {
return Cell {
row: r + translate;
col: c + translate;
}
}
bound function moveToBound(r : Integer, c : Integer)
: Cell {
return Cell {
row: r + translate;
col: c + translate;
}
}
var r = 0;
var c = 0;
var cell1 = bind moveToUnBound(r, c);
var cell2 = bind moveToBound(r, c);
println("cell1: row={cell1.row}, col={cell1.col}");
println("cell2: row={cell2.row}, col={cell2.col}");
r = 5; c = 5;
println("cell1: row={cell1.row}, col={cell1.col}");
println("cell2: row={cell2.row}, col={cell2.col}");
translate = 7;
println("cell1: row={cell1.row}, col={cell1.col}");
println("cell2: row={cell2.row}, col={cell2.col}");
 
Search WWH ::




Custom Search