Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/Generics    [ Add a report in this area ]  
Report #:  122801   Status: Closed
Incompatible types error thrown only for instantiated type
Project:  Delphi Build #:  XE4, XE5
Version:    19.2 Submitted By:   Dalija Prasnikar
Report Type:  Basic functionality failure Date Reported:  2/27/2014 12:28:58 AM
Severity:    Infrequently encountered problem Last Updated: 4/15/2014 6:31:35 PM
Platform:    All platforms Internal Tracking #:   48093
Resolution: Fixed (Resolution Comments) Resolved in Build: : XE6
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: 10
Description
If function result is generic type with class constraint, compiler will allow compiling incorrect function code that returns variable of TObject type, and it will show correct error - Incompatible types - only when generic class is instantated.
Steps to Reproduce:
1. Try compiling attached code - it will pass - expected fail with Incompatible types error

2. Uncomment line marked with 3. - compiling will fail as expected with Incompatible types TObject and TPersistent, but it doesn't show error on the line where is actually happens but at the end of file

3. Comment out line marked with 1. and uncomment line marked with 2. -> code is now correct and will compile

-------------
program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.Classes;

type
  TMyClass = class(TObject)
  protected
    fData: TObject;
  end;

  TMyClass<T: class> = class(TMyClass)
  protected
    function GetData: T;
  public
    property Data: T read GetData;
  end;

function TMyClass<T>.GetData: T;
begin
  Result := fData; // 1. <-- Incompatible types, but code compiles anyway
//  Result := T(fData); // 2. <-- this is how correct code looks like
end;

//var Obj: TMyClass<TPersistent>; // 3. <-- if this line is uncommented then compiler correctly throws Incompatible types TObject and TPersistent

begin
end.
-------------
Workarounds
None
Attachment
None
Comments

None

Server Response from: ETNACODE01