<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sat, Nov 1, 2014 at 9:04 AM, Sven Barth <span dir="ltr"><<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=""><div class="h5">On 31.10.2014 00:04, silvioprog wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Hello,<br>
<br>
Following this article:<br>
<br>
<a href="http://alex.ciobanu.org/?p=51" target="_blank">http://alex.ciobanu.org/?p=51</a><br>
<br>
The compiler does not check the constructor restriction.<br>
<br>
Try this test:<br>
<br>
{$mode delphi}<br>
<br>
   TTest = class<br>
   private // hidding the constructor to cause a compiler error<br>
     constructor Create;<br>
   end;<br>
<br>
   TGen<T: class, constructor> = class<br>
   end;<br>
<br>
constructor TTest.Create;<br>
begin<br>
end;<br>
<br>
var  Gen: TGen<TTest>;<br>
begin<br>
   Gen := TGen<TTest>.Create;<br>
end;<br>
<br>
It compiles well in FPC (from trunk), but the same code in XE is:<br>
<br>
"[dcc32 Error] Unit1.pas(36): E2513 Type parameter 'T' must have one<br>
public parameterless constructor named Create"<br>
<br>
It is a bug in FPC or I need to enable some directive switch?<br>
</blockquote>
<br></div></div>
"constructor" restrictions are not enforced currently, because with the way FPC handles generics compared to Delphi they are a bit useless. Consider this:<br>
<br>
=== code begin ===<br>
<br>
type<br>
  TTest = class<br>
  strict private // just to keep it hidden in the same unit<br>
    constructor Create;<br>
  end;<br>
<br>
var<br>
  t: TTest;<br>
begin<br>
  t := TTest.Create;<br>
end.<br>
<br>
=== code end ===<br>
<br>
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.<br>
<br>
Next snippet:<br>
<br>
=== code begin ===<br>
<br>
type<br>
  TGeneric<T> = class<br>
    procedure Test;<br>
  end;<br>
<br>
procedure TGeneric<T>.Test;<br>
begin<br>
  Writeln(T.Bar);<br>
end;<br>
<br>
begin<br>
end.<br>
<br>
=== code end ===<br>
<br>
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".<br>
<br>
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.<br></blockquote><div><br></div><div>Indded. Thanks for the explanation brother. =)</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
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... :/<br>
</blockquote></div><div><br></div><div>It seems good to check it like Delphi em mode delphi, keeping the compatibility. =/</div><div><br></div>-- <br><div class="gmail_signature">Silvio Clécio<br>My public projects - <a href="http://github.com/silvioprog" target="_blank">github.com/silvioprog</a></div>
</div></div>