[OT] Generics and Type Constraints sample

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[OT] Generics and Type Constraints sample

silvioprog
Hello,

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


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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: [OT] Generics and Type Constraints sample

Sven Barth-2

Am 30.10.2014 20:49 schrieb "silvioprog" <[hidden email]>:
>   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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: [OT] Generics and Type Constraints sample

silvioprog
On Fri, Oct 31, 2014 at 11:23 AM, Sven Barth <[hidden email]> wrote:

Am 30.10.2014 20:49 schrieb "silvioprog" <[hidden email]>:
>   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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: [OT] Generics and Type Constraints sample

etrusco
On Fri, Oct 31, 2014 at 12:08 PM, silvioprog <[hidden email]> wrote:

> On Fri, Oct 31, 2014 at 11:23 AM, Sven Barth <[hidden email]>
> wrote:
>>
>> Am 30.10.2014 20:49 schrieb "silvioprog" <[hidden email]>:
>> >   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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: [OT] Generics and Type Constraints sample

Sven Barth-2
On 01.11.2014 02:16, Flávio Etrusco wrote:

> On Fri, Oct 31, 2014 at 12:08 PM, silvioprog <[hidden email]> wrote:
>> On Fri, Oct 31, 2014 at 11:23 AM, Sven Barth <[hidden email]>
>> wrote:
>>>
>>> Am 30.10.2014 20:49 schrieb "silvioprog" <[hidden email]>:
>>>>    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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: [OT] Generics and Type Constraints sample

silvioprog
In reply to this post by etrusco
On Fri, Oct 31, 2014 at 10:16 PM, Flávio Etrusco <[hidden email]> wrote:
On Fri, Oct 31, 2014 at 12:08 PM, silvioprog <[hidden email]> wrote:
> On Fri, Oct 31, 2014 at 11:23 AM, Sven Barth <[hidden email]>
> wrote:
>>
>> Am 30.10.2014 20:49 schrieb "silvioprog" <[hidden email]>:
>> >   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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal