[fpc-pascal] Generics: constructor restriction fail

Chriss Kalogeropoulos iz.iznogood at gmail.com
Tue Nov 11 23:08:17 CET 2014


Hi all,

I am not sure about the constructor case but I consider the FPC
implementation of generics much more powerful than Delphi's assuming that
Sven's comment actually works as expected.
The fact that the method invocation is actually checked on specialization
and not on declaration means that someone can call methods on the type
parameter directly and not through some interface restriction which IMO is
an ugly hack and cancels all possible optimizations for non virtual
methods, since they much be called through the interface VMT.
On Delphi XE5 I tried to use an interface constraint so that the compiler
won't complaint about the methods and then I tried to call them through an
object variable. This created a compiler internal error, even for class
methods (declared in the interface but called directly from the type
parameter). On FPC however it worked and IMO this is a good feature.

Thanks,
Chriss
Στις 11 Νοε 2014 10:54 μ.μ., ο χρήστης "silvioprog" <silvioprog at gmail.com>
έγραψε:

> On Sat, Nov 1, 2014 at 9:04 AM, Sven Barth <pascaldragon at googlemail.com>
> wrote:
>
>> On 31.10.2014 00:04, silvioprog wrote:
>>
>>> Hello,
>>>
>>> Following this article:
>>>
>>> http://alex.ciobanu.org/?p=51
>>>
>>> The compiler does not check the constructor restriction.
>>>
>>> Try this test:
>>>
>>> {$mode delphi}
>>>
>>>    TTest = class
>>>    private // hidding the constructor to cause a compiler error
>>>      constructor Create;
>>>    end;
>>>
>>>    TGen<T: class, constructor> = class
>>>    end;
>>>
>>> constructor TTest.Create;
>>> begin
>>> end;
>>>
>>> var  Gen: TGen<TTest>;
>>> begin
>>>    Gen := TGen<TTest>.Create;
>>> end;
>>>
>>> It compiles well in FPC (from trunk), but the same code in XE is:
>>>
>>> "[dcc32 Error] Unit1.pas(36): E2513 Type parameter 'T' must have one
>>> public parameterless constructor named Create"
>>>
>>> It is a bug in FPC or I need to enable some directive switch?
>>>
>>
>> "constructor" restrictions are not enforced currently, because with the
>> way FPC handles generics compared to Delphi they are a bit useless.
>> Consider this:
>>
>> === code begin ===
>>
>> type
>>   TTest = class
>>   strict private // just to keep it hidden in the same unit
>>     constructor Create;
>>   end;
>>
>> var
>>   t: TTest;
>> begin
>>   t := TTest.Create;
>> end.
>>
>> === code end ===
>>
>> The above code snippet will compile, because the compiler will use the
>> next best constructor that it can find which is at least the constructor of
>> TObject.
>>
>> Next snippet:
>>
>> === code begin ===
>>
>> type
>>   TGeneric<T> = class
>>     procedure Test;
>>   end;
>>
>> procedure TGeneric<T>.Test;
>> begin
>>   Writeln(T.Bar);
>> end;
>>
>> begin
>> end.
>>
>> === code end ===
>>
>> This will correctly compile in FPC, but not in Delphi, because FPC
>> handles quite some type checking at specialization time not at declaration
>> time. So this will fail to specialize (at compile time) if T does not
>> implement a class method called "Bar".
>>
>> Now let's put these two concepts together and exchange "T.Bar" with
>> "T.Create" (and change the "Writeln" to something else of course ;) ) and
>> it will still compile as long as T provides a Create method. Now as long T
>> is a class this will always be the case, because there is at least the
>> TObject constructor.
>>
>
> Indded. Thanks for the explanation brother. =)
>
>
>> That's why the "constructor" constraint is currently not handled in
>> anyway. That said there'd still be a usecase for this to ensure that there
>> is a default constructor in the class. Also it might be worthwhile to
>> rethink the type checking in case of mode Delphi for generics... :/
>>
>
> It seems good to check it like Delphi em mode delphi, keeping the
> compatibility. =/
>
> --
> Silvio Clécio
> My public projects - github.com/silvioprog
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20141112/0e841fc1/attachment.html>


More information about the fpc-pascal mailing list