Object class: Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<!-- Object class -->
<!-- Object class -->
The Object class is an implicit base class for '''all''' classes. However, until <var class="product">[[Sirius Mods]]</var> 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 <var class="product">[[Sirius Mods]]</var> 8.1.
The <var>Object</var> class is an implicit base class for '''all''' classes. However, until <var class="product">[[Sirius Mods]]</var> 8.1, there was no way to reference the <var>Object</var> base class via object variables of any of its extension classes (that is all classes other than the <var>Object</var> class) and there was no way to assign an variable of one class to a variable of its base <var>Object<var> class. This is all possible in <var class="product">Sirius Mods</var> 8.1.
 
In <var class="product">[[Sirius Mods]]</var> 8.1, it is possible to invoke an Object class method against any object. For example:<p class="code">b


==Invoking Object class methods with non-Object class variables==
In <var class="product">Sirius Mods</var> 8.1, it is possible to invoke an <var>Object</var> class method against any object. For example:<p class="code">b
%foo  is object stringlist
%foo  is object stringlist
%foo = new
%foo = new
printText {~=%foo:className}
printText {~=%foo:[[ClassName (Object function)|className]]}
 
end
end
</p>
</p>


would display:<p class="code">%foo:className=System:Stringlist
would display:<p class="output">%foo:className=System:Stringlist
</p>
</p>


Line 30: Line 28:
</p>
</p>


would display:<p class="code">%pair:className=Pair
would display:<p class="output">%pair:className=Pair
</p>
</p>
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 <var>ClassName</var> to class <var>Pair</var> in the above example, <code>%pair:className</code> would refer to the Pair class <var>className</var>. However, in such a case, it would be possible to reference the Object class <var>className</var> by preceding it with <code>(object)</code> as in:<p class="code">b
These examples illustrate that not only is the <var>Object</var> class an implicit base class of every class (in fact it is not permissible to explicitly declare the <var>Object</var> class as a base class), but every extension class of the <var>Object</var> class inherits the <var>Object</var> class member names. So the <var>Object</var> class does not have to be specified on an <var>Object</var> 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 <var>ClassName</var> is added to class <code>Pair</code> in the above example, <code>%pair:className</code> would refer to the <code>Pair</code> class <code>className</code> method. However, in such a case, it would be possible to reference the <var>Object</var> class <var>ClassName</var> by preceding it with <code>(object)</code> as in:<p class="code">b
class pair
class pair
   public
   public
Line 48: Line 48:
end
end
</p>
</p>
which displays:<p class="code">%pair:className=This name makes no sense
which displays:<p class="output">%pair:className=This name makes no sense
%pair:(object)className=Pair
%pair:(object)className=Pair
</p>
</p>
==Generic Print, Audit, and Trace methods==
<var class="product">Sirius Mods</var> 8.0 introduced generic <var>Print</var>, <var>Audit</var>, and <var>Trace</var> methods that could be applied to all User Language objects. So, to a large degree, these were essentially base <var>Object</var> class methods that could be invoked via User Language object variables. However, they did not behave like true <var>Object</var> class methods, because they were not available to system classes, and because one could not explicitly specify the method's 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 <var>Object</var> class (generic) methods of the same name. These issues are all corrected in <var class="product">Sirius Mods</var> 8.1, so 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 <var>Object</var> variable. For example, in the following, the <code>Pair</code> object variable <code>%pair</code> is assigned to the <var>Object</var> object variable <code>%obj</code>:<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="output">%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 <var>Object</var> class methods are overridable, this is not possible for <var>Object</var> variables.
The assignment of <code>%pair</code> to <code>%obj</code> 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 <code>%obj</code> to an object of the <code>Pair</code> 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 <code>%obj</code> should be narrowed to the more specific <code>Pair</code> 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 <code>PairOfAces</code> were an extension class of <code>Pair</code>, and <code>%aces</code> were a class <code>PairOfAces</code> object variable, the following would '''''not''''' be allowed:<p class="code">...
%obj = %pair
printText {~=%obj:className}
%aces = %obj
printText {~=%p:className}
end
</p>
==Heterogenous collections==
The improved <var>Object</var> support in <var class="product">Sirius Mods</var> 8.1 provides 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 <var>Object</var> objects, and all objects have the <var>Object</var> class as a base class.
The following illustrates a collection that contains objects of two user classes (<code>Frick</code> and <code>Frack</code>), and two system classes (<var>Stringlist</var> and <var>XmlDoc</var>):<p class="code">...
%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
</p>
The above example displays the class name of all the objects in the collection.
==Shared methods==
The <var>Object</var> class contains a large number of shared methods, mostly dealing with the management of [[Global_and_session_objects|global and session objects]]. Before <var class="product>Sirius Mods</var> 8.1, this was the primary use of the <var>Object</var> class.
==Simple Object objects==
It is possible to create a base class <var>Object</var> object that has no extension classes:<p class="code">...
%obj    is object object
%obj = new
</p>
This is the simplest object one can create with the <var class="product">SOUL</var>. As such, it is probably not very useful except, perhaps, as a placeholder.
==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]] shows all the class methods, with a brief description of each.


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

Latest revision as of 21:14, 6 August 2014

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 every extension class of the Object class inherits the Object class member names. So 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 method's 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 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

The improved Object support in Sirius Mods 8.1 provides 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.