Sign in Join

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

Mailing lists / fpc-pascal / 2007-06

FPC's generics

6 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Bisma Jayadi

Hi all,

FPC uses 2 keywords for generics: "generic" for generic definition block, and "specialize" for generic type implementation. I think the "generic" keyword is quite redundant, useless, and too verbose since generics already use pair of to define a generics type. Generics definition should be enough described using the pair of only without "generic" keyword.

The example below should be very obvious to the compiler:

(*** begin of example ***)
type
   TRegularIntegerArray: array[0..100] of integer;
   TGenericArray<T>: array[0..100] of T;
var
   IntegerArrayFromGeneric = specialize TGenericArray<integer>;
(*** end of example ***)

Or, if we MUST use "generic" keyword to help compiler parse the code, also make the syntax less cryptic, I suggest using the example below:

(*** begin of example ***)
type
   TList = generic class(TObject)
     type public
        TCompareFunc = function(const Item1, Item2: T): Integer;
     var public
       data : T;
     procedure Add(item: T);
     procedure Sort(compare: TCompareFunc);
   end of T;
   TGenericArray: generic array[0..100] of T;
var
   PointerListFromGeneric = specialize TList for Pointer;
   IntegerArrayFromGeneric = specialize TGenericArray for Integer;
(*** end of example ***)

Though I agree that a generic MUST have a clear structure definition (in type section) before it's being used (in var section), since generic is all about type definition, also for ease of readability.

Any comments?

-Bee-

has Bee.ography at:
http://beeography.wordpress.com

Jonas Maebe
to Bisma Jayadi
Bisma Jayadi wrote
FPC uses 2 keywords for generics: "generic" for generic definition block, and "specialize" for generic type implementation. I think the "generic" keyword is quite redundant

I think it is useful, because it allows future language extensions to also use the syntax without conflicting with generics (e.g., the Objective Pascal draft syntax also makes use of angle brackets for a couple of things).

Jonas

Bisma Jayadi
to Bisma Jayadi: FPC uses 2 keywords for generics: "generic" for generic definition block, and…
Show quoted text 6 lines
type TRegularIntegerArray: array[0..100] of integer; TGenericArray<T>: array[0..100] of T; var IntegerArrayFromGeneric = specialize TGenericArray<integer>;

Yes, I know, the ':' and '=' usage is wrong, it's a mistypo. :-D

Anyway, using my suggested generic syntax(es), it's allowed to specialize a generic type in type section. Like below example:

Show quoted text 7 lines
type TRegularIntegerArray = array[0..100] of integer; TGenericArray<T> = array[0..100] of T; TIntegerArray = specialize TGenericArray<integer>; var IntegerArray: TIntegerArray;

Or:

Show quoted text 13 lines
type TList = generic class(TObject) type public TCompareFunc = function(const Item1, Item2: T): Integer; var public data : T; procedure Add(item: T); procedure Sort(compare: TCompareFunc); end of T; TPointerList = specialize TList for Pointer; var PointerList: TPointerList;

-Bee-

has Bee.ography at:
http://beeography.wordpress.com

Bisma Jayadi
to Jonas Maebe: I think it is useful, because it allows future language extensions to also use the …
I think it is useful, because it allows future language extensions to also use the syntax without conflicting with generics (e.g., the Objective Pascal draft syntax also makes use of angle brackets for a couple of things).

Then my second syntax proposal might overcome possible conflict with future or other symbol implementation, since it uses no symbol at all, and IMO more pascalish (less cryptic). :-D

In short, my second syntax proposal is:

- generic definition syntax (type section):
   {type_name} = generic {type_definition} of {generic_holder} ;
- generic implementation syntax (var section):
   {var_name} : specialize {generic_type} for {implemented_type} ;
   or if it's defined in type section:
   {type_name} = specialize {generic_type} for {implemented_type} ;

-Bee-

has Bee.ography at:
http://beeography.wordpress.com

Florian Klaempfl
to Bisma Jayadi
Show quoted text 10 lines
> I think it is useful, because it allows future language extensions to > also use the syntax without conflicting with generics (e.g., the > Objective Pascal draft syntax also makes use of angle brackets for a > couple of things). Then my second syntax proposal might overcome possible conflict with future or other symbol implementation, since it uses no symbol at all, and IMO more pascalish (less cryptic). :-D In short, my second syntax proposal is:

Where is the name of the type parameters given? Well and we used < ... > because a lot of programmers recognize it as generic/template definition.

Show quoted text 17 lines
- generic definition syntax (type section): {type_name} = generic {type_definition} of {generic_holder} ; - generic implementation syntax (var section): {var_name} : specialize {generic_type} for {implemented_type} ; or if it's defined in type section: {type_name} = specialize {generic_type} for {implemented_type} ; -Bee- has Bee.ography at: http://beeography.wordpress.com _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal