[fpc-pascal] Generic: restricting a specialized type

silvioprog silvioprog at gmail.com
Sun Feb 8 20:11:21 CET 2015


On Sun, Feb 8, 2015 at 6:41 AM, Sven Barth <pascaldragon at googlemail.com>
wrote:

> 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.
>

Done:

http://bugs.freepascal.org/view.php?id=27424


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

I'll try it.

Thank you! (y)

Regards,
> Sven


-- 
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150208/da43b6ce/attachment.html>


More information about the fpc-pascal mailing list