[fpc-pascal] generics question

Adrian Veith adrian at veith-system.de
Thu May 12 12:37:58 CEST 2011


Hi,

I try this:

type
  TTestGen<T> = class
    constructor Create();
    class function Test(val: T): string; inline;
  end;


function Blah(const val: Integer): string; inline; overload;
begin
  Result:= IntToStr(val + 1);
end;

function Blah(const val: string): string; inline; overload;
begin
  Result:= val + '1';
end;

{ TTestGen }

constructor TTestGen<T>.Create();
begin
end;

class function TTestGen<T>.Test(val: T): string;
begin
  Result:= Blah(val);
end;


type
  TTestInt = TTestGen<Integer>;
  TTestString = TTestGen<String>;     

and get an error: can't determin which overloaded function Blah to use.

It would be nice if this could work. It would be a way to inject inline
functions into a generic class - avoiding virtual functions.

BTW. If I only have one Blah and only one corresponding specialization
it works.

Cheers,

Adrian.



More information about the fpc-pascal mailing list