Difference between revisions of "Generics proposals"

From Lazarus wiki
Jump to navigationJump to search
m
Line 1: Line 1:
Generic Types
 
 
Why
 
Why
 
-------------
 
-------------
Line 12: Line 11:
 
(dannym proposal)
 
(dannym proposal)
  
 +
[code]
 
type
 
type
 
   TGenericList<T> = class
 
   TGenericList<T> = class
Line 17: Line 17:
 
     procedure Add(stuff: T);
 
     procedure Add(stuff: T);
 
   end;
 
   end;
 +
[/code]
  
 
<plugwash> tmycollection=template(tobject)[x]
 
<plugwash> tmycollection=template(tobject)[x]
Line 25: Line 26:
 
------
 
------
 
type user:
 
type user:
the function in the compiler that defines a variable or return type
+
* the function in the compiler that defines a variable or return type
 
and thus 'uses a (already defined) type'.
 
and thus 'uses a (already defined) type'.
  
 
immediate type:
 
immediate type:
not generic and not specialized, i.e. 'normal type'
+
* not generic and not specialized, i.e. 'normal type'
  
 
generic type:
 
generic type:
template for a class with some unspecified types, never to be filled
+
* template for a class with some unspecified types, never to be filled
 
in into this class type. Only by specialisation.
 
in into this class type. Only by specialisation.
  
 
specialisation:
 
specialisation:
copy generic type tree to new specialized type tree,
+
* copy generic type tree to new specialized type tree,
  
 
type parameters:
 
type parameters:
 +
[code]
 
TSomeGeneric<T,Q>
 
TSomeGeneric<T,Q>
T and Q, the unknown types for the generic
+
[/code]
 +
* T and Q, the unknown types for the generic
  
 
specialized type:
 
specialized type:
a generic type with all type parameters know, written into a real
+
* a generic type with all type parameters know, written into a real
 
class type (tree) and reused if possible
 
class type (tree) and reused if possible
  
Line 52: Line 55:
  
 
x. Changes in class definition representation
 
x. Changes in class definition representation
 +
----------------------------------------------
  
 
Each class definition representation has added fields for:
 
Each class definition representation has added fields for:

Revision as of 23:37, 30 December 2004

Why


  • Typesafety
  • Speed
  • Readability

Usage example


(dannym proposal)

[code] type

 TGenericList<T> = class
 public
   procedure Add(stuff: T);
 end;

[/code]

<plugwash> tmycollection=template(tobject)[x] <plugwash> tintegercollection=tmycollection[integer] <oliebol> template mycollection (x: tobject) is more logical

Terms


type user:

  • the function in the compiler that defines a variable or return type

and thus 'uses a (already defined) type'.

immediate type:

  • not generic and not specialized, i.e. 'normal type'

generic type:

  • template for a class with some unspecified types, never to be filled

in into this class type. Only by specialisation.

specialisation:

  • copy generic type tree to new specialized type tree,

type parameters: [code] TSomeGeneric<T,Q> [/code]

  • T and Q, the unknown types for the generic

specialized type:

  • a generic type with all type parameters know, written into a real

class type (tree) and reused if possible


Changes


x. Changes in class definition representation


Each class definition representation has added fields for:

 - class instantiation mode
    0  immediate type
    1  generic type, no instantiation, just generate specialized type
    2  specialized type
 - when mode 2:
     generic type uplink (what, unitname, typename?)
   when mode 1:
     list of specialized types known so far (and where)
   when mode 0: 
     nothing

x. Changes in 'type user' compilation (for class types only)

Each class type user will have to check mode of the class type. If mode is immediate

 - proceed as always till now.

If mode is specialized

 - proceed as always till now 
 - keep in mind the generic type for some checks later (and debugging).

If mode is generic, this:

 - check 'list of specialized types' for the type parameter to use.
   If there is already a specialized type, use that.
   (given that it is compatible with 'compile parameters' XXXX)
 - If not available, clone the generic type in the tree, and fill in the
   type params with the actual types to use.
   Write that out to XXXX?