Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2005-11

Re: Templates / Generics Syntax

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Thomas Schatzl
Show quoted text 26 lines
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.

No, already after the second token after the opening "<". It must either be a ",", or the closing bracket.

Assuming that you disallow something like generic_type<generic_type2<...

Even that limitation can be overcome by disallowing directly nested generic use specification. Something like

type
	TGeneric2 = generic_type2<...>;

generic_type<TGeneric2 ...

could be allowed however.

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.

Simply use a second, third, ... lookahead token for the < case.

Show quoted text 6 lines
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.

From: Mattias Gaertner

Show quoted text 26 lines
On Thu, 3 Nov 2005 19:59:40 +0100 (CET) Daniël Mantione wrote: Right. I didn't think of that. What about edged brackets? type TGenericClass[T,F] = class public procedure Add(Item: T; Flag: F); end; procedure TGenericClass.Add(Item: T; Flag: F); // Note: No redundant [T,F] after TGenericClass. begin end; type TListOfComponent = TGenericList[TComponent]; Analog: type TGenericListClass[T] = class of TGenericList[T]; type PGenericRecord[T] = ^TGenericRecord[T]; procedure GenericProc[T](Param: T); begin end;

I think this has a similar problem to the other one. Consider this:

my_procedure[...

where my_procedure isn't an instantiation of a method using the generic but a function returing an array of something.

Not completely sure whether this is a problem though... depends on the implementation.

Regards,
Thomas

Reply by email

Mattias Gaertner
to Thomas Schatzl
Show quoted text 30 lines
> 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. No, already after the second token after the opening "<". It must either be a ",", or the closing bracket.

.. or '.'. E.g. generic<unit.type>

Show quoted text 10 lines
Assuming that you disallow something like generic_type<generic_type2<... Even that limitation can be overcome by disallowing directly nested generic use specification. Something like type TGeneric2 = generic_type2<...>; generic_type<TGeneric2 ... could be allowed however.

I second that. We should try to avoid anonymous types (generic<generic<type>>). It's bad programming style.

Show quoted text 35 lines
[...] From: Mattias Gaertner > On Thu, 3 Nov 2005 19:59:40 +0100 (CET) > Daniël Mantione wrote: > Right. I didn't think of that. > > What about edged brackets? > > type > TGenericClass[T,F] = class > public > procedure Add(Item: T; Flag: F); > end; > > procedure TGenericClass.Add(Item: T; Flag: F); > // Note: No redundant [T,F] after TGenericClass. > begin > end; > > type TListOfComponent = TGenericList[TComponent]; > > Analog: > type TGenericListClass[T] = class of TGenericList[T]; > type PGenericRecord[T] = ^TGenericRecord[T]; > > procedure GenericProc[T](Param: T); > begin > end; I think this has a similar problem to the other one. Consider this: my_procedure[... where my_procedure isn't an instantiation of a method using the generic but a function returing an array of something.

You mean for example:
function my_function: TList;
where TList has a default indexed property.

Ok, so (! !) would be better.
But see other mails, about where Delphi goes. It seems they will use the syntax.

Not completely sure whether this is a problem though... depends on the implementation.

Mattias

Reply by email