2

I'm trying to compile the following code in Delphi Rio:

unit untObjectHelper;

interface

uses
   SysUtils;

type
   TObjectHelper = class(TInterfacedObject)
   public
      class procedure Clone(const objOrigem: TObject; const objDestino: TObject);
   end;

implementation

uses
   System.Rtti;

{ TObjectHelper }

class procedure TObjectHelper.Clone(const objOrigem,
   objDestino: TObject);
begin
   if not Assigned(objOrigem) then
      Exit;

   if not Assigned(objDestino) then
      Exit;

   if objOrigem.ClassType <> objDestino.ClassType then
      Exit;

   var contexto := TRttiContext.Create;
   try
      var tipo := contexto.GetType(objOrigem.ClassType);
      var campos := tipo.GetFields();
   finally
      contexto.Free;
   end;
end;

end.

however the following error occurs:

[dcc32 Fatal Error] untObjectHelper.pas (36): F2084 Internal Error: NC1921

on the line:

var fields: = type.GetFields ();

version: Embarcadero® Delphi 10.3 Version 26.0.33219.4899

I did not find reference to this error, could someone help me? thank you very much

Passella
  • 640
  • 1
  • 8
  • 23

1 Answers1

2

The problem is the type inference, thanks to Rudy Velthuis for the tip

unit untObjectHelper;

interface

uses
   SysUtils;

type
   TObjectHelper = class(TInterfacedObject)
   public
      class procedure Clone(const objOrigem: TObject; const objDestino: TObject);
   end;

implementation

uses
   System.Rtti;

{ TObjectHelper }

class procedure TObjectHelper.Clone(const objOrigem,
   objDestino: TObject);
begin
   if not Assigned(objOrigem) then
      Exit;

   if not Assigned(objDestino) then
      Exit;

   if objOrigem.ClassType <> objDestino.ClassType then
      Exit;

   var contexto := TRttiContext.Create;
   try
      var tipo := contexto.GetType(objOrigem.ClassType);
      var campos: TArray<TRttiField> := tipo.GetFields();
   finally
      contexto.Free;
   end;
end;

end.
Passella
  • 640
  • 1
  • 8
  • 23
  • 1
    Alternatively, you can use your original code with the following amendment: `var campos: TArray := tipo.GetFields();`. The problem is not the inline declaration, the problem is the type inference, which doesn't work properly. – Rudy Velthuis May 06 '19 at 16:42