Base Constructors
Constructors in a Class hierarchy are not inherited in the same way as other members. However, there is a mechanism for all the Classes in the Class inheritance tree to participate in the initialisation of an Instance.
Every Constructor function contains a :Implements Constructor
statement which may appear anywhere in the function body. The statement may optionally be followed by the :Base
control word and an arbitrary expression.
The statement:
:Implements Constructor :Base expr
calls a monadic Constructor in the Base Class. The choice of Constructor depends upon the rank and shape of the result of expr
(see Constructor Overloading for details).
Whereas, the statement:
:Implements Constructor
or
:Implements Constructor :Base
calls the niladic Constructor in the Base Class.
Note that during the instantiation of an Instance, these calls potentially take place in every Class in the Class hierarchy.
If, anywhere down the hierarchy, there is a monadic call and there is no matching monadic Constructor, the operation fails with a LENGTH ERROR
.
If there is a niladic call on a Class that defines no Constructors, the niladic call is simply repeated in the next Class along the hierarchy.
However, if a Class defines a monadic Constructor and no niladic Constructor it implies that that Class cannot be instantiated without Constructor arguments. Therefore, if there is a call to a niladic Constructor in such a Class, the operation fails with a LENGTH ERROR
. Note that it is therefore impossible for APL to instantiate a fill item or process a reference to an empty array for such a Class or any Class that is based upon it.
A Constructor function may not call another Constructor function and a constructor function may not be called directly from outside the Class or Instance. The only way a Constructor function may be invoked is by ⎕NEW
. The fundamental reason for these restrictions is that there must be one and only one call on the Base Constructor when a new Instance is instantiated. If Constructor functions were allowed to call one another, there would be several calls on the Base Constructor. Similarly, if a Constructor could be called directly it would potentially duplicate the Base Constructor call.