The objects and classes that you define in your program are of intrinsic class TadsObject. Everything that has “object” as its superclass is really a subclass of intrinsic class TadsObject.
For example:
class Item: object;
myObj: object;
Both Item and myObj are of intrinsic class TadsObject.
TadsObject is a subclass of the root intrinsic class, Object, so all of the methods that Object defines are inherited by TadsObject instances as well. In addition to the Object methods, TadsObject provides its own methods, described below.
createClone() - creates a new object that is an identical copy of this object. The new object will have the same superclasses as the original, and the identical set of properties defined in the original.
No constructor is called in creating the new object, since the object is explicitly initialized by this method to have the exact property values of the original.
The clone is a "shallow" copy of the original, which means that the clone refers to all of the same objects as the original. For example, if a property of the original points to a Vector, the corresponding property of the clone points to the same Vector, not a copy of the Vector.
createInstance(…) – creates a new instance of this object; that is, creates a new object whose superclass is the “self” object of this method. This method’s arguments are passed directly to the constructor, if any, of the new object; this method doesn’t make any other use of the arguments. The method creates the object, invokes the new object’s constructor, then returns the new object.
This method can be especially useful in static methods defined in base classes that are further subclassed, because it essentially allows a parameterized “new” operator. For example, suppose we had a base class, Coin, which you subclass into several types: GoldCoin, SilverCoin, CopperCoin. For each of these classes, you want to provide a method that creates a new instance of that kind of coin. Using the “new” operator, you’d have to write a separate method in each subclass:
class Coin: object;
class GoldCoin: Coin
createCoin() { return new GoldCoin(); }
;
class SilverCoin: Coin
createCoin() { return new SilverCoin(); }
;
class CopperCoin: Coin
createCoin() { return new CopperCoin(); }
;
This gets increasingly tedious as we add new subclasses. What we’d really like to do is something like this:
class Coin: object
createCoin() { return new self(); } // illegal!
;
This would let all the subclasses inherit this one implementation, which would create the appropriate kind of object depending on the subclass on which the method was invoked. We can’t write exactly this code, though, because the “new” operator doesn’t allow a variable like “self” to be used as its argument.
So, it’s createInstance() to the rescue. This method lets us do exactly what we’d like: create an instance of the current class, writing the code only once in the base class. Using createInstance(), we can rewrite the method to get the effect we want:
class Coin: object
createCoin() { return createInstance(); }
;
createTransientInstance(…) – this works like createInstance(), except that the new instance is transient.