SourceForge.net LogoThe Eiffel Compiler / Interpreter (tecomp)

doc/lang/reference/genericity

Genericity

There are simple classes and generic classes. INTEGER is a simple class and ARRAY is a generic class. You can derive generic types from generic classes. Generic types are e.g.

   ARRAY[INTEGER]
   ARRAY[REAL]
   ARRAY[AIRPLANE]
   ARRAY[POINT]
 

Generic types can be arbitrarily nested like e.g.

   ARRAY[ARRAY[INTEGER]]
 

In ARRAY[INTEGER], the type INTEGER is called an actual generic. The class ARRAY is defined as

   class ARRAY[G]  ... end
 

and the parameter G is called a formal generic.

Formal Generics

Formal generics are

  Formal_generics: "[" {Formal_generic "," ...}+ "]"
  Formal_generic:   ["frozen"] Identifier [Constraint] 

Each formal generic can have an optional constraint to which an actual generic has to conform.

  Constraint:          "->" Type [Constraint_creators]
  Constraint_creators: "create" Feature_list "end"

The keyword frozen limits conformance of generic types. Normally C[Y] conforms to C[X] if Y conforms to X. However if C is declared class C[frozen G] ... end, C[Y] conforms to C[X] only if Y = X.

Generic Types

From a generic class class C[G1,G2,...] ... end with the formal generics G1, G2, ... it is possible to derive generic types C[AG1,AG2,...] with the actual generics AG1, AG2, ...

The actual generics AG1, AG2, ... have to satisfy the Generic derivation rule (VTGD):

Example:

   class C[  G, 
             H->COMPARABLE,     
             I->CONSTRAINT 
                  create proc1, proc2 end,
             J->ARRAY[like Current] -- invalid, because of the anchored type
          ]
   ...
   feature
       create_an_i
          local
            v: I
          do
            create v.proc1(...)
            ...
          end
   end
 

Class C has 4 formal generics G, H, I and J. The formal generic G is unconstrained, i.e. it can be substituted by any type.

The second formal generic H can only be substituted by types conforming to COMPARABLE, i.e. to types which have inherited conformantly (possibly renamed) all features from the class COMPARABLE, e.g. less_than alias "<", greater_than alias ">", ....

The third formal generic I can be substituted by any type conformant to CONSTRAINT. The type constraint must have the features proc1 and proc2. Any actual generic substituted for I has the (possible renamed) features proc1 and proc2, since it conforms to CONSTRAINT. In addition to that, it has to have its version of the features proc1 and proc2 available for creation to class C.

Because of the last condition, the feature "create_an_i" can create an object of type I for any actual generic substituted for I.

The forth formal generic J is invalid, because its constraint involves the anchored type like Current.

 Local Variables: 
 mode: outline
 coding: iso-latin-1
 outline-regexp: "=\\(=\\)*"
 End:
Table of contents

- Formal Generics

- Generic Types


ip-location