[fpc-devel] Templates / Generics Syntax

Marc Weustink marc.weustink at cuperus.nl
Fri Nov 4 14:24:13 CET 2005


Mattias Gaertner wrote:
> On Fri, 04 Nov 2005 13:44:55 +0100
> Marc Weustink <marc.weustink at cuperus.nl> wrote:
> 
> 
>>Mattias Gaertner wrote:
>>
>>>On Fri, 04 Nov 2005 10:47:42 +0100
>>>Marc Weustink <marc.weustink at cuperus.nl> wrote:
>>>
>>>
>>>
>>>>Daniƫl Mantione wrote:
>>>>
>>>>
>>>>>Op Thu, 3 Nov 2005, schreef Mattias Gaertner:
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>>Here is a proposal of the syntax:
>>>>>>
>>>>>>type
>>>>>>TGenericClass<T,F> = class
>>>>>>public
>>>>>>  procedure Add(Item: T; Flag: F);
>>>>>>end;
>>>>>
>>>>>
>>>>>This syntax is almost impossible to implement since in one of your
>>>
>>>other >>
>>>
>>>>>mails the symbols to mark the parameters appear in regular source code:
>>>>>
>>>>>begin
>>>>> generictypeA<integer>.create
>>>>>end.
>>>>>
>>>>>It will be very hard for the parser to see the difference in advance 
>>>>>between:
>>>>>
>>>>> variable<integer(another_var)
>>>>> generic_type<integer
>>>>>
>>>>>Only when the > symbol is parsed the result is known.
>>>>>
>>>>>Maybe the parser may be able lookup the type first and make a decision 
>>>>>based on that, but it would be in the middle of a recursive expression 
>>>>>parse designed for infix operators.
>>>>>
>>>>>Also in C++ this sometimes causes trouble where you need to change your
>>>
>>>>>code notation so the compiler eats it. I don't know if this proposal 
>>>>>suffers from this mess, but we should avoid at all cost to import it
>>>>>into  Pascal.
>>>>
>>>>On the wiki pages some of my remarks got lost (or I didn't write them 
>>>>down) but when I first looked at the <> notation style it looks very 
>>>>unpascalish for me. I like more to add a new keyword for it, like the 
>>>>samples wiht generic or template)
>>>>
>>>>BTW,
>>>>what woud be the problem with
>>>>
>>>>type
>>>>  TMySpecificClass = TGenericClass(TObject, Integer);
>>>
>>>
>>>What about proc generics:
>>>
>>>GenericProc(TObject)
>>>
>>>This can be ambigious.
>>
>>How would you have declared the proc, and how do you call it. I don't 
>>see a problem yet.
>>
>>GenericProc(TObject)(some params) you mean ?
> 
> 
> Example:
> 
> procedure MyProc(T); // generic procedure without parameters
> ver i: T;
> begin
>   ...
> end;

Therefor I propose it to use the generic keyword ->

generic procedure MyProc(T); // generic procedure without parameters
var i: T;
begin
   ...
end;


> procedure MyProc(T: TClass); // non generic procedure
> begin
> end;

This should give a duplicate Identifier error.


or when we usae a syntax similar Micha proposes (I changed the of):


<code>
type
   TGenericProc = generic(T) of procedure(param, param)
   ... use T
   end;


   TObjectProc = TGenericProc(TObject)

generic(T) procedure MyGenericProc(...)
begin
end;
</code>


<code>
type
   TGenericCollection = generic(T: TCollectionItem) of class(TComponent)
   ...implement TCollection and use T
   end;

   TCollection = TGenericCollection(TCollectionItem);
   TFieldDefs = TGenericCollection(TFieldDef);
</code>

And:

<code>
type
   TGenericList = generic(T: PtrInt) of class(TObject)
   ...implement TList and use PtrInt size for code generation
   end;

   TList = TGenericList(Pointer);
</code>


Marc




More information about the fpc-devel mailing list