Water 5-Advanced Abstractions-Meta-fieldContract| Parameter key | Default value | Type | | container | opt | | Parameter kind | Default value | Type | | Other unkeyed arguments | opt | | Other keyed arguments | opt | | Water Contract<class field
container=opt
_other_keyed=opt=wob="_add_to_environment"
_other_unkeyed=opt=wob=ekind.code="_add_to_environment"
/> | |
See also: field_key
Create a field:
number.<field "integer"/>
Get the object that the field represents:
number.<field "integer"/>.value
 | number.integer |
Background
Objects are made up of fields.
Each field has a key and a value.
The key and the value may be any object though
usually the key is a string or an integer.
Meta-fields
Water has special machinery to attach "meta data" to
fields by creating "meta fields".
These meta fields are meant to hold data about a field,
which is not necessarily about the VALUE of the field but
has more to do about what kind of information may be in
the field. the Water system has meta fields for "type"
and "doc" to help users and system software know about potential
values for fields. Developers may add their own meta fields
using names appropriate to the domain of their application.
Meta fields must be named by a string.
Syntax
Meta fields are actually just regular fields that have
a key that is a string composed of the key of the field,
the string "_f_" and the meta key itself.
Example: weight_f_type
You can create meta fields in a number of ways.
The most common is when defining a method's parameter that
has a type:
<class vehicle/>
<method vehicle.drive speed=65=integer> </method>
Makes a meta field in vehicle of speed_f_type
whose value is integer.
vehicle.drive.speed_f_type
integer
You can also directly set a meta field via:
thing.<set foo_f_bar = 23/>
thing.foo_f_bar
 | 23 |
You can avoid knowing the details of the metafield infix "_f_"
by using the method field_key to make the key:
<field_key "foo" "bar"/>
"foo_f_bar" thing.<set <field_key "foo" "bar"/> = 23/>
thing.<get <field_key "foo" "bar"/>/>
 | 23 |
Restrictions
The meta_key itself, i.e. "type" must be a string and cannot
start with underscore.
You also can't have a meta_key of "key" , "value" ,
or "container" .
Field Objects
Most of the time the above knowledge is all you'll need to know
about meta fields. But for more advanced work, you may find it
convenient to think about a field as an actual object
with keys of the meta-keys of the field.
In order to make a field object, use the field class to
make a field instance. Each field object must have a key
and may have either a container or some other meta-key fields.
Field Objects with a Container
thing.<set weight=48 weight_f_type=integer/>
thing.weight
 | 48 |
thing.weight_f_type
integer <set my_field = thing.<field "weight"/>/>
my_field.<get "type"/>
 | integer |
my_field.<get "value"/>
48
(Note: "value" is the default meta_key. It stands for the regular field value.)
my_field.<set doc="how heavy it is"/>
my_field.<get "doc"/>
 | "how heavy it is" |
thing.weight_f_doc
"how heavy it is"
When a field object has a container, it becomes a proxy for the
field within its containing object.
A field object with a container specifies a location as an object
and the key within the object. This can be used to get, set and test
if the field exists within the object.
Implementation
Field objects have special definitions for the methods:
has , get , and set to achieve their behavior.
© Copyright 2007 Clear Methods, Inc.