Object class

From m204wiki
Revision as of 18:42, 6 August 2014 by JAL (talk | contribs) (minor cleanup)
Jump to navigation Jump to search

The Object class is an implicit base class for all classes. However, until Sirius Mods 8.1, there was no way to reference the Object base class via object variables of any of its extension classes (that is all classes other than the Object class) and there was no way to assign an variable of one class to a variable of its base Object class. This is all possible in Sirius Mods 8.1.

Invoking Object class methods with non-Object class variables

In Sirius Mods 8.1, it is possible to invoke an Object class method against any object. For example:

b %foo is object stringlist %foo = new printText {~=%foo:className} end

would display:

%foo:className=System:Stringlist

And

b class pair public variable x is float variable y is float end public end class %pair is object pair %pair = new printText {~=%pair:className} end

would display:

%pair:className=Pair

These examples illustrate that not only is the Object class an implicit base class of every class (in fact it is not permissible to explicitly declare the Object class as a base class) but that every extension class of the Object class inherits the Object class member names so that the Object class does not have to be specified on an Object class member reference.

However, as with other extension classes, all classes can "hide" a base class member by containing a member with the same name. For example, if the member ClassName is added to class Pair in the above example, %pair:className would refer to the Pair class className method. However, in such a case, it would be possible to reference the Object class ClassName by preceding it with (object) as in:

b class pair public variable x is float variable y is float variable classname is string len 32 end public end class %pair is object pair %pair = new %pair:className = 'This name makes no sense' printText {~=%pair:className} printText {~=%pair:(object)className} end

which displays:

%pair:className=This name makes no sense %pair:(object)className=Pair

Generic Print, Audit, and Trace methods

Sirius Mods 8.0 introduced generic Print, Audit, and Trace methods that could be applied to all User Language objects. So, to a large degree, these were essentially base Object class methods that could be invoked via User Language object variables. However, they did not behave like true Object class methods because they were not available to system classes and because one could not explicitly specify the methods class via (object) before the Print, Audit, or Trace method name. This meant that if a class had one or more of these methods already defined, there was no way to get at the Object class (generic) methods of the same name. These issues are all corrected in Sirius Mods 8.1 so that the Print, Audit, and Tracemethods are full-fledged members of the Object class.

Widening and narrowing assignment

As noted before, all object variables except enumerations and method variables can be assigned to an Object variable. For example, in the following, the Pair object variable %pair is assigned to the Object object variable %obj:

b class pair public variable x is float variable y is float variable classname is string len 32 end public end class %pair is object pair %obj is object object %pair = new %pair:className = 'This name makes no sense' %obj = %pair printText {~=%obj:className} end

This displays:

%obj:className=Pair

Note that the extension class ClassName method is not invoked here, because extension class methods are never invoked directly via a base class variable, unless the extension class method implements a base class method. But since no Object class methods are overridable, this is not possible for Object variables.

The assignment of %pair to %obj above, as all assignments of any object class variable to an Object class variable, is a widening assignment: that is, an assignment from a more specific class object to a more generic class. Since Object is a base class of all object classes it is thus always possible to assign an object variable to an object variable of the Object class.

It is also possible to narrow an Object class object variable to any other object class variable. In the above example, it would be possible to assign %obj to an object of the Pair class:

b ... %pair is object pair %pair2 is object pair %obj is object object %pair = new %pair:className = 'This name makes no sense' %obj = %pair printText {~=%obj:className} %pair2 = %obj:(pair) printText {~=%pair2:className} end

This uses standard narrrowing assignment syntax in indicating that %obj should be narrowed to the more specific Pair class. As with all narrowing assignments, this means that the assignment can fail because the Object class variable does not reference an object of the class being narrowed to.

It's worth pointing out that the Object class acts like an Allow Narrow class: that is, narrowing assignments are allowed for Object class variables. It would seem silly for Object class variables to not allow narrowing assignments, as that would largely defeat the purpose of being able to assign to Object class variables. In fact, narrowing assignment is such an important aspect of Object class variables, that not only is narrowing assignment allowed, but it can be done implicitly (with one limitation). The above example could have been written as:

... %obj = %pair printText {~=%obj:className} %pair2 = %obj printText {~=%pair2:className} end

The limitation mentioned above is that an implicit narrowing assignment is not allowed to an extension class of the variable used to originally set the object variable. For example, if class PairOfAces were an extension class of Pair, and %aces were a class PairOfAces object variable, the following would not be allowed:

... %obj = %pair printText {~=%obj:className} %aces = %obj printText {~=%p:className} end

Heterogenous collections

A capability enabled by the improved Object support in Sirius Mods 8.1 is the ability to create collections with items from many different classes. Of course, this is because the collection would be defined as a collection of Object objects and all objects have the Object class as a base class.

The following illustrates a collection that contains objects of two user classes (Frick and Frack), and two system classes (Stringlist and XmlDoc):

... %frick is object frick %frack is object frack %sl is object stringlist %doc is object xmlDoc %objects is arraylist of object object %i is float ... %objects = new %objects:add(%frick) %objects:add(%sl) %objects:add(%doc) %objects:add(%frack) for %i from 1 to %objects:count printText Item {%i} is a {%objects(%i):className} end for

The above example displays the class name of all the objects in the collection.

Shared methods

The Object class contains a large number of shared methods, mostly dealing with the management of global and session objects. Before Sirius Mods 8.1, this was the primary use of the Object class.

Simple Object objects

It is possible to create a base class Object object that has no extension classes:

... %obj is object object %obj = new

This is the simplest object one can create with the SOUL. As such, it is probably not very useful except, perhaps, as a placeholder.

List of Object methods

The List of Object methods shows all the class methods, with a brief description of each.