Sign in Join

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

Mailing lists / fpc-pascal / 2009-02

Usage of Generics in FPC

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Graeme Geldenhuys

Hi,

I've been playing around with Generics for a few hours - simply to see what all the fuss is about.
I was given the following Delphi link to help explain generics. http://sjrd.developpez.com/delphi/tutoriel/generics/

I noticed very early on that FPC has a very different sytax to Delphi. I guess that's because FPC implemented generics first? The catch-22 case. Do we innovate and be the first (of our competitors) to support generics, or do we wait and simply follow the crowd. Obviously there are pros and cons for both cases. Are there plans to change the syntax to match Delphi? I don't want to implement many things now, simply to have to change them in the future when the syntax gets updated.

Anyway, I found the "fgl.pp" unit which contains a generic list: TFPGList class. Does FPC include any other classes for use with generics?

Why can't I do the following:

procedure WriteSquares(AMax: Integer);
var
  List: specialize TFPGList<integer>;
begin
  List := TFPGList<integer>.Create;

...instead I have to ALWAYS declare a type first....

type
  TIntList = specialize TFPGList<Integer>;
procedure WriteSquares(AMax: Integer);
var
  List: TIntList;
begin
  List := TIntList.Create;

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Jonas Maebe
to Graeme Geldenhuys
Graeme Geldenhuys wrote 5 lines
I was given the following Delphi link to help explain generics. http://sjrd.developpez.com/delphi/tutoriel/generics/ I noticed very early on that FPC has a very different sytax to Delphi. I guess that's because FPC implemented generics first?

Yes.

Are there plans to change the syntax to match Delphi?

I don't know. But at this time, further generics development appears to be at a standstill anyway (Peter has little time for FPC development nowadays).

Anyway, I found the "fgl.pp" unit which contains a generic list: TFPGList class. Does FPC include any other classes for use with generics?

fgl is the only unit with generics classes shipped with FPC at this time.

Show quoted text 20 lines
Why can't I do the following: procedure WriteSquares(AMax: Integer); var List: specialize TFPGList<integer>; begin List := TFPGList<integer>.Create; ...instead I have to ALWAYS declare a type first.... type TIntList = specialize TFPGList<Integer>; procedure WriteSquares(AMax: Integer); var List: TIntList; begin List := TIntList.Create;

Probably to simplify the parser (to reduce ambiguities with the parser has to decide whether you want to perform a comparison or instantiate a generic). There may even be undecidable cases otherwise.

Jonas

Florian Klaempfl
to Graeme Geldenhuys: I've been playing around with Generics for a few hours - simply to see what all the fuss…
Show quoted text 20 lines
Why can't I do the following: procedure WriteSquares(AMax: Integer); var List: specialize TFPGList<integer>; begin List := TFPGList<integer>.Create; ...instead I have to ALWAYS declare a type first.... type TIntList = specialize TFPGList<Integer>; procedure WriteSquares(AMax: Integer); var List: TIntList; begin List := TIntList.Create;

You can't do

longintpointer:=^longint(p);

in pascal either but you need

type
plongint = ^longint;
...
longintpointer:=plongint(p);

so it's imo logical that generics must be specialized explicitly before they can be used. Same applies e.g. for parameter types too.

Graeme Geldenhuys
to Florian Klaempfl
Florian Klaempfl wrote 14 lines
You can't do longintpointer:=^longint(p); in pascal either but you need type plongint = ^longint; ... longintpointer:=plongint(p); so it's imo logical that generics must be specialized explicitly before they can be used. Same applies e.g. for parameter types too.

Just asked, because the following IS possible in Delphi. Either way, explicit type (as was shown in my second example) is slightly neater for me as well.

uses
  SysUtils, Classes, Generics.Collections;
procedure WriteSquares(Max: Integer);
var
  List: TList<Integer>;
  I: Integer;
begin
  List := TList<Integer>.Create;
  try
    for I := 0 to Max do
      List.Add(I*I);
....

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/