Using the passed-by-reference nature of arrays, it is possible to use an
array to represent a data structure (similar to a C struct passed by pointer)
that is passed to functions that manipulate it:
This helps implementing data driven programming:
To ease using the above pattern, fawk implements the "." operator
that is a shorthand syntax for an array indexing with the string
version of the field name. The following two array references
are equivalent:
Using this syntax the above example cn be rewritten into a more
readable form:
The next logical step is to bundle the functions that can manipulate the
data, to the same array. This is possible since function references can
be saved in variables or in array members (see section 4.6.):
It is possible to set up a "base class" for the color structure so that
different object instances can be created more easily:
Obviously "multiple inheritance" can be implemented the same way, simply using
two loops for copying fields from two different "base classes":
There are many major features of OOP missing: there are no automatisms,
everything needs to be done by the user, in code. Most notably there is
no constructor or destructor and passing the object as the first argument
is done manually, there is no "this" in functions.
6.2. The "struct syntax" of array indexing
arrname.fieldname
arrname["fieldname"]
arrname.fieldname
arrname['fieldname']
6.3. Using an array as an object
6.4. Inheritance: "base class"
6.5. Why it is not real OOP