Sign in Join

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

Mailing lists / fpc-pascal / 2014-10

[OT] Generics and Type Constraints sample

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

silvioprog

Hello,

I tried this example (Lazarus 1.3 r46702 FPC 2.7.1 x86_64-linux-gtk 2; FPC from trunk too):

http://blogs.teamb.com/craigstuntz/2008/08/29/37832

and got the error:

Compile Project, Target: project1: Exit code 256, Errors: 3
unit1.pas(27,22) Error: Identifier not found "T"
unit1.pas(27,23) Error: Type identifier expected
unit1.pas(27,24) Error: Class or interface type expected, but got
"<erroneous type>"

The code:

unit Unit1;
//{$mode objfpc}{$H+}
{$mode delphi}
interface
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;
type
  TAdder<T> = class
  public
    function AddEm(A, B: T): T; virtual; abstract;
  end;
  TIntAdder = class(TAdder<integer>)
  public
    function AddEm(A, B: integer): integer; override;
  end;
  TStringAdder = class(TAdder<string>)
  public
    function AddEm(A, B: string): string; override;
  end;
  TMath<T; A: TAdder<T>, constructor> = class
  public
     function AddTwoDigits(Left, Right: T): T;
  end;
  { TForm1 }
  TForm1 = class(TForm)
    procedure Button1Click(Sender: TObject);
  end;
var
  Form1: TForm1;
implementation
{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  AddInt: TMath<integer, TIntAdder>;
  AddString: TMath<string, TStringAdder>;
begin
  AddInt := TMath<integer, TIntAdder>.Create;
  try
    ShowMessage(IntToStr(AddInt.AddTwoDigits(2, 2)));
  finally
    AddInt.Free;
  end;
  AddString := TMath<string, TStringAdder>.Create;
  try
    ShowMessage(AddString.AddTwoDigits('2', '2'));
  finally
    AddString.Free;
  end;
end;

{ TIntAdder }

function TIntAdder.AddEm(A, B: integer): integer;
begin
  Result := A + B;
end;

{ TStringAdder }

function TStringAdder.AddEm(A, B: string): string;
begin
  Result := IntToStr(StrToInt(A) + StrToInt(B));
end;

{ TMath<T, A> }

function TMath<T, A>.AddTwoDigits(Left, Right: T): T;
var
  Add: A;
begin
  Add := A.Create;
  try
    Result := Add.AddEm(Left, Right);
  finally
    Add.Free;
  end;
end;
end.
Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Sven Barth
to silvioprog
"silvioprog" wrote
TMath<T; A: TAdder<T>, constructor> = class

It is not yet possible to reuse a type parameter directly in the parameter declaration. There already exists a bug report though.

Regards,
Sven

Reply by email

silvioprog
to Sven Barth
Sven Barth wrote 9 lines
Am 30.10.2014 20:49 schrieb "silvioprog" : > TMath<T; A: TAdder<T>, constructor> = class It is not yet possible to reuse a type parameter directly in the parameter declaration. There already exists a bug report though. Regards, Sven

Very nice!

Some link? I will follow it.

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Flávio Etrusco
to silvioprog
silvioprog wrote 16 lines
On Fri, Oct 31, 2014 at 11:23 AM, Sven Barth wrote: > > Am 30.10.2014 20:49 schrieb "silvioprog" : > > TMath<T; A: TAdder<T>, constructor> = class > > It is not yet possible to reuse a type parameter directly in the parameter > declaration. There already exists a bug report though. > > Regards, > Sven Very nice! Some link? I will follow it.

Probably this: http://bugs.freepascal.org/view.php?id=25678 It was easy to find just searching "generics" in unresolved FPC tickets ;-)

-Flávio

Reply by email

Sven Barth
to Flávio Etrusco
Flávio Etrusco wrote 20 lines
On Fri, Oct 31, 2014 at 12:08 PM, silvioprog wrote: > On Fri, Oct 31, 2014 at 11:23 AM, Sven Barth > wrote: >> >> Am 30.10.2014 20:49 schrieb "silvioprog" : >>> TMath<T; A: TAdder<T>, constructor> = class >> >> It is not yet possible to reuse a type parameter directly in the parameter >> declaration. There already exists a bug report though. >> >> Regards, >> Sven > > Very nice! > > Some link? I will follow it. > Probably this: http://bugs.freepascal.org/view.php?id=25678 It was easy to find just searching "generics" in unresolved FPC tickets ;-)

Yes, that's the one. Thanks for searching.

Regards,
Sven

Reply by email

silvioprog
to Flávio Etrusco: Probably this: http://bugs.freepascal.org/view.php?id=25678 It was easy to find just…
Flávio Etrusco wrote 24 lines
On Fri, Oct 31, 2014 at 12:08 PM, silvioprog wrote: > On Fri, Oct 31, 2014 at 11:23 AM, Sven Barth < > > wrote: >> >> Am 30.10.2014 20:49 schrieb "silvioprog" : >> > TMath<T; A: TAdder<T>, constructor> = class >> >> It is not yet possible to reuse a type parameter directly in the parameter >> declaration. There already exists a bug report though. >> >> Regards, >> Sven > > Very nice! > > Some link? I will follow it. > Probably this: http://bugs.freepascal.org/view.php?id=25678 It was easy to find just searching "generics" in unresolved FPC tickets ;-) -Flávio

Thanks a lot guy!

Silvio Clécio My public projects - github.com/silvioprog

Reply by email