Extending Object Definitions

When we are building libraries, we often run into situations where we would like to add functionality to an object that was originally created in a different library. We do this by "extending" the object definition. Typically, we extend an object definition either to add additional clauses to the object or to add additional renderings.

Suppose we have defined an object named MyCommute in a library named Library1.

Now we create a new library named Library2 and a new object named MyObject. With MyObject defined as:

We want MyObject to be commutative and we want to use the MyCommute function to implement that functionality .

To do this, we create an Object Module in Library2. Add the Math namespace and then create an object named MyCommute with the same type definition as the definition of MyCommute in Library1 where the object was originally defined. Next add a clause to MyCommute in the module in Library2:

Note that the MyCommute object has an entry listed under Extensions. The entry refers to the module in Library1 that has the original definition of the MyCommute object. The object definition in Library1 also has an extension that refers to the module in Library2.

Extensions are only shown for object definitions where both the names and the type definitions are identical.

After we build the document, we will be able to use the MyCommute function on MyObject. We can see this be creating a unit test in Library2. To do this, create a new unit test module in Library2 and edit the unit test to look like this:

When an object definition has extensions and more than one of them contains clauses, the clauses are added in the reverse order to the order that the libraries appear in the Object Explorer. So in this case, the clause in Library2 is executed before any clauses in Library1.

In exactly the same way, we can extend an object definition to add a rendering. Any object definition can be extended, not just those that are functions.