Bindings
var a = 5;
var b = bind a; // b has now the value ofa
assert(b == 5);
a = 2;
assert(b == 2); // changes of a appear in b
b = 1; // direct modification not allowed
var c = 3;
b = bind c; // binding only in initialization
def d = bind a; // def and var are equivalent for bind
def e = bind a + c; // binding to an expression (more)
assert(e == 5);
a = 5;
assert(e == 8);
c = 10;
assert(e == 15);
def f = bind { // bind block
def x = if (a > 0) then 10 else -10;
c = 10 // no side-effects allowed
var y = 1; // no variables allowed
x * 10;
};
assert(f == 100);
var v = 2;
var multiplicator = 10;
function mul(x) { // regular function
x * multiplicator
}
bound function mulBound(x) { // bound function
x * multiplicator
}
def a = bind mul(v);
def b = bind mulBound(v);
assert(a == 20 and b == 20);
v = 3; // after argument changes all functions recalculate
assert(a == 30 and b == 30);
multiplicator = 5; // only the bound function changes
assert(a == 30 and b == 15);
v = 3; multiplicator = 5;
def c = bind mul(v) + mulBound(v); // bound function in expression
assert(c == 30);
v = 4;
assert(c == 40);
multiplicator = 10;
assert(c == 60); // only the bound function recalculates
class Point {
var x : Integer; var y : Integer;
}
var a = 5;
def p1 = bind Point{x: a y: 2}; // bind object
assert(p1.x == 5);
def p1Old = p1;
a = 17;
assert(p1.x == 17); // whole object changed
assert(not isSameObject(p1, p1Old)); // new instance created
def p2 = Point{x: bind a y: 2}; // bind property
assert(p2.x == 17);
def p2Old = p2;
a = 25;
assert(p2.x == 25); // property changed
assert(isSameObject(p2, p2Old)); // same old instance

