Chapter 6: Fake objects and structs (arrays)

6.1. Using an array as a context struct

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:

6.2. The "struct syntax" of array indexing

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:

arrname.fieldname
arrname["fieldname"]
arrname.fieldname
arrname['fieldname']

Using this syntax the above example cn be rewritten into a more readable form:

6.3. Using an array as an object

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.):

6.4. Inheritance: "base class"

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":

6.5. Why it is not real OOP

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.