Question

I'm trying to use generic records with RTTI, but ran into a problem with Type Info. Does anyone know why the following won't compile using Delphi 2010?

program GenericTypeInfo;

{$APPTYPE CONSOLE}

uses
  TypInfo,
  SysUtils;

type

  TMyRec<T> = record
  public
    Value: T;
  end;

  TMyInt = TMyRec<Integer>;
  TMyString = TMyRec<String>;

begin

  try
    Writeln(GetTypeName(TypeInfo(TMyRec<Integer>)));     <--- This works fine
    Writeln(GetTypeName(TypeInfo(TMyRec<String>)));      <---   so does this
    Writeln(GetTypeName(TypeInfo(TMyInt)));              <--- BUT this won't compile
    Writeln(GetTypeName(TypeInfo(TMyString)));           <---   nor this!!
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;

  Readln;

end.

The lines indicated above generate the following compiler errors:

[DCC Error] GenericTypeInfo.dpr(24): E2134 Type 'TMyInt' has no type info
[DCC Error] GenericTypeInfo.dpr(24): E2134 Type 'TMyString' has no type info

I can't what's the big difference between the 2? I admit I'm not a low-level expert, but why does the compiler treat this differently? I need it to work for the TMyInt and TMyString types.

Thanks for any help.

Was it helpful?

Solution

This is a bug in Delphi 2010 which has been fixed for XE and higher.

But there is a workaround.

OTHER TIPS

This example works fine in XE2. (And XE as @StefanGlienke comments).

RTTI and generics in Delphi-2010 lacks implementation in many ways.

Since the interpretation of types is done by compiler magic, the workaround is to upgrade.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top