[fpc-pascal] Generic: restricting a specialized type

Sven Barth pascaldragon at googlemail.com
Sun Feb 8 10:41:58 CET 2015


On 08.02.2015 04:10, silvioprog wrote:
> Hello,
>
> I'm trying to port a Delphi code to FPC, and it has a structure like:
>
>    TType = class(TObject)
>    end;
>
>    TTest<T1; T2: TType> = class(TObject)
>    end;
>
>    TFoo = class(TType)
>    end;
>
>    TBar = class(TTest<string, TFoo>)
>    end;
>
> I.e., T1 accepts any type, but T2 accepts _only_ TType and/or their
> descendants.
>
> I tried the following code in FPC:
>
>    TType = class(TObject)
>    end;
>
>    generic TTest<T1; T2: TType> = class(TObject)
>    end;
>
>    TFoo = class(TType)
>    end;
>
>    TBar = class(specialize TTest<string, TFoo>)
>    end;
>
> But:
>
> ===
> Compile Project, Target: project1.exe: Exit code 1, Errors: 2
> Unit1.pas(30,34) Error: class type expected, but got "AnsiString"
> Unit1.pas(30,47) Error: class type expected, but got "<erroneous type>"
> ===
>
> If I declare "T2" instead of "T2: TType", it compiles, but using this
> declaration, a programmer can use accidentally a non descendant type of
> TType.
>
> Is there an issue about this in bugtracker?

Looks like a bug. Please report.

As a workaround you can move the constraint to the first type instead of 
the second and specialize with "<TFoo, string>".

Regards,
Sven




More information about the fpc-pascal mailing list