|
Spec-Zone .ru
спецификации, руководства, описания, API
|
The onReplaceClause, if present, causes the block to be executed each time the value of the variable changes -- including the assignment of its initial value.
The optional parameters provide information about what is changed: on replace oldValue [ firstIndex .. lastIndex ] = newElements { ... }
But these parameters are all optional, and in the simplest form none of these are present:
var x = 0 on replace { println("x is now: {x}") }
The oldValue parameter provides the previous value.
var x = 0 on replace oldValue { println("x was {oldValue} and is now: {x}") }
The remaining parameters are useful with sequences.
var seq = ['A', 'B', 'C', 'D', 'E', 'F'] on replace oldValue[firstIdx .. lastIdx] = newElements {
println("replaced {oldValue}[{firstIdx}..{lastIdx}] by {newElements} yielding {seq}")
}
seq[3] = '$';
insert '#' into seq;
delete '$' from seq;
delete seq[2];
seq[1..4] = ['X', 'Y'];
delete seq;
Which prints:
replaced [0..-1] by ABCDEF yielding ABCDEF replaced ABCDEF[3..3] by $ yielding ABC$EF replaced ABC$EF[6..5] by # yielding ABC$EF# replaced ABC$EF#[3..3] by yielding ABCEF# replaced ABCEF#[2..2] by yielding ABEF# replaced ABEF#[1..4] by XY yielding AXY replaced AXY[0..2] by yielding
Note that firstIndex, lastIndex and newElements refer only to the portion of the sequence which has changed,
[To do: explain concept more. Note that it is inferior to binding]
[To do: brief intro to slices, plus cross-reference]