Water 5-Advanced Abstractions-Active ValuesContract| No input parameters. | | Parameter kind | Default value | Type | | Other unkeyed arguments | opt with ekind of expression | | Water Contract<class active_value
_other_unkeyed=opt=wob=ekind.expression=wob="_other_unkeyed"
/> | |
You can set a variable such that instead of just returning
the value in the field, it will execute arbitrary code instead.
thing.<set test_av=
<thing head_weight=200
tail_weight=300
weight=<active_value> thing.test_av.head_weight.<plus thing.test_av.tail_weight/>
</active_value>
/> /> thing.test_av.head_weight
200 thing.test_av.tail_weight
300 thing.test_av.weight
500 thing.test_av.<set head_weight=222/>
thing.test_av.weight
522
You can also set local and stack variables to active values.
When a call to active_value is executed, a special object is created that captures both the
expressions in the content of the call as well as the local environment that the call was
made in. So the content of active_value can contain references to local variables
in the environment of the call.
When a references to a variable that contains an active value is made, the
expressions in the content of the creating make-active_value are executed in
their original local environment.
We can use active_value to set up an "alias" like so:
thing.test_av.<set orig=27/>
thing.test_av.orig
 | 27 |
thing.test_av.<set clone=<active_value> thing.test_av.orig</active_value> />
thing.test_av.clone
 | 27 |
thing.test_av.<set orig=28/>
thing.test_av.orig
 | 28 |
thing.test_av.clone
28
Regardless of which one(s) are set to what, both will have the same value.
Within the body of active_value , _subject refers to the object that
was called with the make active value.
Example:
<class boat hull_weight=10 sail_weight=20
total_weight=<active_value>
_subject.hull_weight.<plus _subject.sail_weight/>
</active_value>
/>
<boat sail_weight=40/>.total_weight | 50 |
Here's why:
First an instance of boat is created, shadowing the sail_weight from boat of 20 with 40.
Then we ask for the total_weight where we get the hull_weight from boat because it
isn't shadowed (10) and the sail_weight from the instance (40) for 10 + 40 = 50
Free local variables in the content of a call to active_value will execute to the
values that they had when the call to active_value was made, just like a closure.
© Copyright 2007 Clear Methods, Inc.