Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.

Public Report
Report From: Delphi-BCB/Compiler/Delphi/Generics    [ Add a report in this area ]  
Report #:  140960   Status: Reported
E2071 This type cannot be initialized When you try to declare a constant "array" of the generic type
Project:  Delphi Build #:  21.0.17707.5020
Version:    21.0 Submitted By:   Peter Overweel
Report Type:  Basic functionality failure Date Reported:  3/21/2017 2:05:39 AM
Severity:    Commonly encountered problem Last Updated: 3/21/2017 3:06:01 AM
Platform:    All versions Internal Tracking #:  
Resolution: None  Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
Description
This issue very much resembles resolved Report #: 111755.
The difference is that the constant is not a sole constant, but an const "array":

When you try to declare a constant "array" of the generic type, you get errors:

E2071 This type cannot be initialized

...
  TTest2<T> = record
  private
    Data: T;
  end;
...
...
const
  Test22: array[0..0] of TTest2<Integer> = ((Data: 10)); //[dcc32 Error] E2071 This type cannot be initialized
...

//Note that this kind of initialization is perfectly legal when no generic type is used
Steps to Reproduce:
-------------
program Project11;
{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils;

type
  TTest1 = record
  private
    Data: Integer;
  end;

  TTest2<T> = record
  private
    Data: T;
  end;

  TTest3 = TTest2<Integer>;

var
  Test1: array[0..0] of TTest1 = ((Data: 10)); //it's OK
  Test2: array[0..0] of TTest2<Integer> = ((Data: 10));  //[dcc32 Error] E2071 This type cannot be initialized
  Test3: array[0..0] of TTest3 = ((Data: 10)); //[dcc32 Error] E2071 This type cannot be initialized

const
  Test11: array[0..0] of TTest1 = ((Data: 10);)  //it's OK
  Test22: array[0..0] of TTest2<Integer> = ((Data: 10)); //[dcc32 Error] E2071 This type cannot be initialized
  Test33: array[0..0] of TTest3 = ((Data: 10)); //[dcc32 Error] E2071 This type cannot be initialized

begin
  try
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
Workarounds
None
Attachment
None
Comments

None

Server Response from: ETNACODE01