Object class: Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
Line 53: Line 53:
==Generic Print, Audit, and Trace Methods==
==Generic Print, Audit, and Trace Methods==
<var class="product">[[Sirius Mods]]</var> 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 <code>(object)</code> before the <var>Print</var>, <var>Audit</var>, or <var>Trace</var> 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 <var class="product">[[Sirius Mods]]</var> 8.1 so that the <var>Print</var>, <var>Audit</var>, and <var>Trace</var>methods are full-fledged members of the <var>Object</var> class.
<var class="product">[[Sirius Mods]]</var> 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 <code>(object)</code> before the <var>Print</var>, <var>Audit</var>, or <var>Trace</var> 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 <var class="product">[[Sirius Mods]]</var> 8.1 so that the <var>Print</var>, <var>Audit</var>, and <var>Trace</var>methods are full-fledged members of the <var>Object</var> class.
==Widening and Narrowing Assignment==
As noted before, all object variables except [[Enumerations|enumerations]] and [[Method_variables|method variables]] can be assigned to an Object variable. For example, in the following, the Pair object variable <var>%pair</var> is assigned to the Object object variable <var>%obj</var>:<p class="code">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
</p>
This displays:<p class="code">%obj:className=Pair
</p>
Note that the extension class <var>className</var> 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 <var>Object</var> variables.
The assignment of <var>%pair</var> to <var>%obj</var> above, as all assignments of '''any''' object class variable to an <var>Object</var> class variable, is a widening assignment, that is, an assignment from a more specific class object to a more generic class. Since <var>Object</var> is a base class of all object classes it is thus always possible to assign an object variable to an object variable of the <var>Object</var> class.
It is also possible to [[Narrowing_assignments_and_class_tests|narrow]] an <var>Object</var> class object variable to any other object class variable. In the above example, it would be possible to assign <var>%obj</var> to an object of the <var>Pair</var> class:<p class="code">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
</p>
This uses standard [[Narrowing_assignments_and_class_tests|narrrowing assignment]] syntax in indicating that <var>%obj</var> should be narrowed to the more specific <var>Pair</var> class. As with all narrowing assignments, this means that the assignment can fail because the <var>Object</var> class variable does not reference an object of the class being narrowed to.
It's worth pointing out that the <var>Object</var> class acts like an <var>Allow Narrow</var> class, that is, narrowing assignments are allowed for <var>Object</var> class variables. It would seem silly for <var>Object</var> class variables to not allow narrowing assignments as that would largely defeat the purpose of being able to assign to <var>Object</var> class variables. In fact, narrowing assignment is such an important aspect of <var>Object</var> 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:<p class="code">...
%obj = %pair
printText {~=%obj:className}
%pair2 = %obj
printText {~=%pair2:className}
end
</p>
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 <var>PairOfAces</var> were an extension class of <var>Pair</var>, and <var>%aces<var> were a class <var>PairOfAces</var> object variable, the following would '''not''' be allowed:<p class="code">...
%obj = %pair
printText {~=%obj:className}
%aces = %obj
printText {~=%p:className}
end
</p>
==List of Object methods==
==List of Object methods==
The [[List of Object methods|"List of Object methods"]] shows all the class methods, with a brief description of each.
The [[List of Object methods|"List of Object methods"]] shows all the class methods, with a brief description of each.


[[Category:System classes]]
[[Category:System classes]]

Revision as of 04:10, 16 July 2012

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.

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 we add the member ClassName to class Pair in the above example, %pair:className would refer to the Pair class className. 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

List of Object methods

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