Java Reference
In-Depth Information
demonstrates how one could use binding with a conditional expression to, in
effect, compute the maximum value between two variables. In this case, the
bound variable
max
will always contain the larger of the values of
a
and
b
. When-
ever
a
or
b
change, the conditional expression (
a > b
) will be reevaluated poten-
tially resulting in the update of
max
. The output of the code above yields
max = 2
max = 3
max = 4
A block expression in JavaFX is a sequence of zero or more statements with a
terminating expression enclosed in curly braces. Binding to a block expression
takes on this general syntax:
var v = bind { [
statement
;]*
expression
}
As will become important when
bound functions
are discussed later, the termi-
nating expression is the block's return value. In reality, bound blocks are quite
restricted in functionality in that the statements inside a bound block are limited
to variable declarations only. When trying to use any other type of statement
(i.e., assignment,
insert
,
delete
, etc.), a compilation error will result. So let's
run though a few examples to see what can and can't be done with bound block
expressions:
class Cell {
var row : Integer;
var col : Integer;
}
var r : Integer = 0;
var c : Integer = 0;
var extra : Integer = 0;
var cell1 = bind Cell { row: r, col: c } // legal
var cell2 = bind { Cell {row: r, col: c } } // legal
var cell3 = bind { var a : Integer = 3; // legal
Cell {row: r*a, col: c }
}
var cell4 = bind { extra = 1;
// ILLEGAL
Cell {row: r, col: c }
}
Search WWH ::
Custom Search