Description
In this reference section we discuss how to make new types of object and new objects of those types.
installing a new method for new-of-from
-
- Usage:
new AA of BB from C := (A,B,c) -> ...
-
Inputs:
-
AA, a hash table
-
BB, a hash table
-
C, a hash table
-
(A,B,c) -> ..., a function of 3 arguments: AA will be an ancestor of A, BB will be an ancestor of B, and C will be an ancestor of the class of c. Alternatively, A will be a type of AA, B will be a type of BB, and c will be an instance of C.
-
Outputs:
-
the function is returned as the value of the expression
-
Consequences:
-
the function will be installed as the method for new AA of BB from C. It will be stored under the key (NewOfFromMethod,AA,BB,C) in the youngest of the hash tables AA, BB, and C.
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, html => f };
|
The hash tables AA, BB, and C will normally be instances of Type.
new-of-from
-
- Usage:
new A of B from c
-
Inputs:
-
Outputs:
-
the new object of class A and with parent B described above
-
Consequences:
-
the function previously installed as the method for new A of B from C will be called with arguments (A,B,c).
-
if no such method has been installed, then ancestors of A, B, and C, will be consulted, searching lexicographically for a method; see inheritance.
-
if no method is found by searching the ancestors, then the function (A,B,c) -> c will be used
-
the value returned by the function, (or c, if no method was found), will have its class set to A and its parent set to B; see newClass.
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{html => f}
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}
|
installing a new method for new-of
-
- Usage:
new AA of BB := (A,B) -> ...
-
Inputs:
-
AA, a hash table
-
BB, a hash table
-
(A,B) -> ..., a function of 2 arguments: AA will be an ancestor of A, and BB will be an ancestor of B. Alternatively, A will be a type of AA, and B will be a type of BB.
-
Outputs:
-
the function is returned as the value of the expression
-
Consequences:
-
the function will be installed as the method for new AA of BB. It will be stored under the key (NewOfMethod,AA,BB) in the younger of the hash tables AA and BB.
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 = FunctionClosure[stdio:11:25-14:41]
o11 : FunctionClosure
|
new-of
-
- Usage:
new A of B
-
Inputs:
-
Outputs:
-
the new object of class A and parent B described above
-
Consequences:
-
the function previously installed as the method for new A of B will be called with arguments (A,B).
-
if no such method has been installed, then ancestors of A and B will be consulted, searching lexicographically for a method; see inheritance.
-
the value returned by the function will have its class set to A and its parent set to B; see newClass.
-
if no method is found by searching the ancestors, then a new empty instance of A with parent B will be created
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
|
installing a new method for new-from
-
- Usage:
new AA from C := (A,c) -> ...
-
Inputs:
-
AA, a hash table
-
C, a type
-
(A,c) -> ..., a function of 2 arguments: AA will be an ancestor of A, and C will be an ancestor of the class of c. Alternatively, A will be a type of AA and c will be an instance of C.
-
Outputs:
-
the function is returned as the value of the expression
-
Consequences:
-
the function will be installed as the method for new AA from C. It will be stored under the key (NewFromMethod,AA,C) in the younger of the hash tables AA and C.
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 = FunctionClosure[stdio:21:17-21:33]
o18 : FunctionClosure
|
new-from
-
- Usage:
new A from c
-
Inputs:
-
Outputs:
-
the new object of class A initialized from c described above
-
Consequences:
-
the function previously installed as the method for new A from C will be called with arguments (A,c).
-
if no such method has been installed, then ancestors of A and C, will be consulted, searching lexicographically for a method; see inheritance.
-
if no method is found by searching the ancestors, then the function (A,c) -> c will be used
-
the value returned by the function, (or c, if no method was found), will have its class set to A and its parent retained; see newClass
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
|
installing a new method for new
-
- Usage:
new AA := (A) -> ...
-
Inputs:
-
AA, a hash table
-
(A) -> ..., a function of 1 argument: AA will be an ancestor of A. Alternatively, A will be a type of AA.
-
Outputs:
-
the function is returned as the value of the expression
-
Consequences:
-
the function will be installed as the method for new AA. It will be stored under the key NewMethod in the hash table AA.
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 = FunctionClosure[stdio:24:9-24:30]
o21 : FunctionClosure
|
new
-
- Usage:
new A
-
Inputs:
-
Outputs:
-
the new object of class A described above
-
Consequences:
-
the function previously installed as the method for new A will be called with argument A.
-
if no such method has been installed, then ancestors of A will be consulted, searching for a method; see inheritance.
-
the value returned by the function will have its class set to A; see newClass
-
if no method is found by searching the ancestors, then a new empty instance of A will be created, if possible
We use the method for new M installed above.
i22 : new M
o22 = M{a, b, c}
o22 : M
|