In this reference section we discuss how to make new types of object and new objects of those types.
In this example we install a creation method for new types of types from functions. The function is used to construct the method for converting instances of the new type to nets for display.
i1 : new Type of BasicList from Function := (A,B,f) -> hashTable { net => f };
|
The hash tables AA, BB, and C will normally be instances of Type.
We use the creation method installed above to create a new type of list.
i2 : f = s -> "--list of type X--" o2 = f o2 : FunctionClosure |
i3 : X = new Type of List from f o3 = X o3 : Type |
i4 : class X o4 = Type o4 : Type |
i5 : parent X o5 = List o5 : Type |
i6 : peek X
o6 = Type of List{net => f}
|
Now we use new to create a new list of type X from a list. The system knows how to convert lists to lists of type X, so no creation method need be installed for new X from List.
i7 : x = new X from {1,3,11,12}
o7 = --list of type X--
o7 : X
|
i8 : class x o8 = X o8 : Type |
i9 : parent x o9 = Nothing o9 : Type |
i10 : peek x
o10 = {1, 3, 11, 12}
|
This operation turns out to be needed infrequently, because there is no from-clause to provide data for initializing the instance of A.
i11 : new Type of BasicList := (type,array) -> (
stderr << "--new " << type << " of "
<< array << " being made" << endl;
new MutableHashTable)
o11 = -*Function[stdio:11:39-14:25]*-
o11 : FunctionClosure
|
We illustrate this operation by making a new type of basic list, and then by making a list of that type.
i12 : M = new Type of BasicList --new Type of BasicList being made o12 = M o12 : Type |
i13 : m = new M from {3,4,5}
o13 = M{3, 4, 5}
o13 : M
|
i14 : class m o14 = M o14 : Type |
i15 : m#1 o15 = 4 |
Now let's define a method for reversing the elements of a list of class M, using the unary operator -.
i16 : - M := reverse o16 = reverse o16 : CompiledFunction |
i17 : - m
o17 = M{5, 4, 3}
o17 : M
|
Let's use the class M defined above, and introduce a method for creating lists of class M from integers. Then we use it in the subsection below.
i18 : new M from ZZ := (M',i) -> 0 .. i o18 = -*Function[stdio:21:25-21:33]*- o18 : FunctionClosure |
We use the new-from method for new M from ZZ installed above.
i19 : n = new M from 13
o19 = M{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
o19 : M
|
i20 : - n
o20 = M{13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
o20 : M
|
We use the class M introduced above, and install a method for new M, and we use it in the next subsection.
i21 : new M := (M') -> {"a","b","c"}
o21 = -*Function[stdio:24:15-24:15]*-
o21 : FunctionClosure
|
We use the method for new M installed above.
i22 : new M
o22 = M{a, b, c}
o22 : M
|