Java Reference
In-Depth Information
Our final example demonstrates the full power of triggers as it deals with an
assignment of a sequence slice:
var s1 : Integer[] = [ 2,4,6,8,10 ] on replace
s1Orig[begin..end]=n
{
s2[begin..end] = s1[begin..end];
}
var s2 : Integer[];
print("s1: "); println(s1);
print("s2: "); println(s2);
s1[0..2] = [3,6,9];
print("s1: "); println(s1);
print("s2: "); println(s2);
delete s1[0..2];
print("s1: "); println(s1);
print("s2: "); println(s2);
The trigger contains five arbitrarily named variables, and in the context of this
example, they have the following meanings:
s1
is the sequence after all the changes have been applied.
s1Orig
represents the previous value of the
s1
sequence. As such, it is the
same type as
s1
.
begin
is the first index of the sequence slice that has been updated.
end
is the last index of the sequence slice that has been updated. Both
begin
and
end
are
Integer
types.
n
represents the sequence of values that will be replacing the slice of
s1Orig[begin..end]
.
The output of this code looks like this:
s1: [ 2, 4, 6, 8, 10 ]
s2: [ ]
s1: [ 3, 6, 9, 8, 10 ]
s2: [ 3, 6, 9 ]
s1: [ 8, 10 ]
s2: [ 8, 10 ]
For our final example, we'll contrast how
bind
and
on replace
behave when
instance variables are inherited and overridden from a superclass. The code
block that follows contains two classes, the first of which, called
MyClass
, con-
tains a
public name
variable that is both bound and has a trigger. The second
Search WWH ::
Custom Search