Previous | Table of Contents | Next |
When a series of parameters share the same mode, type, and default, Name can be a list of identifiers separated by commas. Such a list is shorthand for a list in which the mode and type are repeated for each identifier:
Mode v1, ..., vn: Type := Default
is shorthand for
Mode v1: Type := Default; ...; Mode vn: Type := Default
This shorthand is eliminated from the expanded definition of the type. The default values are included.
A procedure value P is a member of the type T if it is NIL or its signature is covered by the signature of T, where signature1 covers signature2 if
The parameter names and defaults affect the type of a procedure, but not its value. For example, consider the declarations:
PROCEDURE P(txt: TEXT := P) = BEGIN Wr.PutText(Stdio.stdout, txt) END P; VAR q: PROCEDURE(txt: TEXT := Q) := P;
Now P = q is TRUE, yet P() prints P and q() prints Q. The interpretation of defaulted parameters is determined by a procedures type, not its value; the assignment q := P changes qs value, not its type.
The following are examples of procedure types:
TYPE Integrand = PROCEDURE (x: REAL): REAL; Integrator = PROCEDURE(f: Integrand; lo, hi: REAL): REAL; TokenIterator = PROCEDURE(VAR t: Token) RAISES {TokenError}; RenderProc = PROCEDURE( scene: REFANY; READONLY t: Transform := Identity)
In a procedure type, RAISES binds to the closest preceding PROCEDURE. That is, the parentheses are required in
TYPE T = PROCEDURE (): (PROCEDURE ()) RAISES {}
An object is either NIL or a reference to a data record paired with a method suite, which is a record of procedures that will accept the object as a first argument.
An object type determines the types of a prefix of the fields of the data record, as if OBJECT were REF RECORD. But in the case of an object type, the data record can contain additional fields introduced by subtypes of the object type. Similarly, the object type determines a prefix of the method suite, but the suite can contain additional methods introduced by subtypes.
If o is an object, then o.f designates the data field named f in os data record. If m is one of os methods, an invocation of the form o.m( ... ) denotes an execution of os m method. An objects methods can be invoked but not read or written.
If T is an object type and m is the name of one of Ts methods, then T.m denotes Ts m method. This notation makes it convenient for a subtype method to invoke the corresponding method of one of its supertypes.
A field or method in a subtype masks any field or method with the same name in the supertype. To access such a masked field, use NARROW to view the subtype variable as a member of the supertype, as illustrated later in this chapter.
Object assignment is reference assignment. Objects cannot be dereferenced because the static type of an object variable does not determine the type of its data record. To copy the data record of one object into another, the fields must be assigned individually.
There are two predeclared object types:
ROOT | {The traced object type with no fields or methods} |
UNTRACED ROOT | {The untraced object type with no fields or methods} |
The declaration of an object type has the form
TYPE T = ST OBJECT Fields METHODS Methods OVERRIDES Overrides END
where ST is an optional supertype, Fields is a list of field declarations, exactly as in a record type, Methods is a list of method declarations, and Overrides is a list of method overrides. The fields of T consist of the fields of ST followed by the fields declared in Fields. The methods of T consist of the methods of ST modified by Overrides and followed by the methods declared in Methods. T has the same reference class as ST.
The names introduced in Fields and Methods must be distinct from one another and from the names overridden in Overrides. If ST is omitted, it defaults to ROOT. If ST is untraced, then the fields must not include traced types. (This restriction is lifted in unsafe modules.) If ST is declared as an opaque type, the declaration of T is legal only in scopes where STs concrete type is known to be an object type.
The keyword OBJECT can optionally be preceded by BRANDED or by BRANDED b, where b is a text constant. The meaning is the same as in non-object reference types.
A method declaration has the form
m sig := proc
where m is an identifier, sig is a procedure signature, and proc is a top-level procedure constant. It specifies that Ts m method has signature sig and value proc. If := proc is omitted, := NIL is assumed. If proc is non-nil, its first parameter must have mode VALUE and type some supertype of T, and dropping its first parameter must result in a signature that is covered by sig.
A method override has the form
m := proc
where m is the name of a method of the supertype ST and proc is a top-level procedure constant. It specifies that the m method for T is proc, rather than ST.m. If proc is non-nil, its first parameter must have mode VALUE and type some supertype of T, and dropping its first parameter must result in a signature that is covered by the signature of STs m method.
Previous | Table of Contents | Next |