|
|
@@ -61,17 +61,28 @@ mismatch). Suite: `GenericProg`, `PairProg`, `BufProg`, `g_badbuf`,
|
|
|
```
|
|
|
|
|
|
So **no standalone `GENERIC CLASS` syntax is required** for the
|
|
|
- capability. Standalone syntax would only matter if a class must be
|
|
|
- instantiated *without* a whole generic module; the principled form
|
|
|
- for that is a **type refinement** (`TYPE IntStack = Stack(INTEGER);`),
|
|
|
- which mirrors `MODULE X = G(args)` at type level but needs the
|
|
|
- class's `CLASS IMPLEMENTATION` block re-parsed/renamed per instance —
|
|
|
- a design of its own. Recommendation: keep the generic-module pattern.
|
|
|
+ capability. It also composes with the increment-3 features: suite
|
|
|
+ `GStkProg` instantiates a class whose **array field is sized by a
|
|
|
+ value parameter** and whose element is the type parameter:
|
|
|
|
|
|
- Minor asymmetry noted: a class is accepted as a top-level item in an
|
|
|
- IMPLEMENTATION/program `DeclSeq` but not in a DEFINITION unit's item
|
|
|
- list (only inside a `TYPE` block) — harmless, but a tidy-up
|
|
|
- candidate.
|
|
|
+ ```modula2
|
|
|
+ GENERIC DEFINITION MODULE GStk (T : TYPE; cap : CARDINAL);
|
|
|
+ TYPE
|
|
|
+ CLASS Stk;
|
|
|
+ a : ARRAY [0..cap] OF T;
|
|
|
+ PROCEDURE Capacity() : INTEGER;
|
|
|
+ END Stk;
|
|
|
+ END GStk.
|
|
|
+ (* class impl: *) PROCEDURE Capacity() : INTEGER; BEGIN RETURN HIGH(a) END Capacity;
|
|
|
+ DEFINITION/IMPLEMENTATION MODULE GIntStk = GStk(INTEGER, 4); (* HIGH(a)=4 *)
|
|
|
+ ```
|
|
|
+
|
|
|
+ Small print for the pattern: a class must be declared **inside a
|
|
|
+ `TYPE` block** (a bare `CLASS` item is not accepted in a DEFINITION
|
|
|
+ unit's item list); the `CLASS IMPLEMENTATION` block is a `DeclSeq`
|
|
|
+ item, so it needs a **trailing `;`**; and a class impl's `BEGIN`
|
|
|
+ initialiser cannot assign a *field* (only module globals) — so
|
|
|
+ field state must be set by a method, as above.
|
|
|
|
|
|
## Résumé
|
|
|
|