There's still things I don't understand with Generic :/

There's still things I don't understand with Generic :/

type
  TValues = array of record
    Key  : string;
    Value: T;
  end;

  TIntegerValues = TValues;

procedure TForm1.FormCreate(Sender: TObject);
var
  V: TIntegerValues;
begin
  SetLength(V, 1);
  V[0].Key := 'TEST';
  V[0].Value := 1;  // E2010 Incompatible types 'T' and 'Integer'
end;

Comments

  1. What happens if you change:

      V:TIntegerValues; 

    to be

      V:TValues;

    ?

    ReplyDelete
  2. Compiler bug, happens in XE and XE8. Report it and change your code as follows:

    type
      TValue = record
        Key: string;
        Value: T;
      end;

      TValues = array of TValue;

    or

      TValues = array of TPair;

    or

      TIntegerValues = TArray>;

    ReplyDelete
  3. Or like this

    type
      TValue = record
        Key  : string;
        Value: T;
      end;

    procedure Foo;
    var
      V: TArray>;
    begin
      SetLength(V, 1);
      V[0].Key := 'TEST';
      V[0].Value := 1;  // E2010 Incompatible types 'T' and 'Integer'
    end;

    to avoid needing to make an alias

    ReplyDelete
  4. While we're at it, personally I prefer the TArray <> variant in favor of an explicit "array of", as it makes it easier to use with other generic functions.

    ReplyDelete
  5. ...and source code perfectly compiled by FPC. (Sarcasm) Nice, Embarcadero!

    ReplyDelete
  6. Is the problem present in 8.1 as well?

    ReplyDelete
  7. When I say XE8 that includes subscription update 1 of course :)

    ReplyDelete
  8. I fear that the upcoming migration to XE8.1 just came to a grinding halt.

    ReplyDelete
  9. Lars Fosdal  Why? This bug is an ever present.

    ReplyDelete
  10. it seems to me that the way Generics are handled is a bit complex, after all there's no underlying framework like .Net to care about, so basicaly Generic could be just some kind of macro

    ReplyDelete
  11. Paul TOTH Indeed. Over 15000 bugs are still waiting to be fixed by Embarcadero Technologies

    ReplyDelete
  12. David Heffernan - Did I misunderstand? I read Stefan saying it was in XE and XE8?

    ReplyDelete
  13. José Ramírez - Why don't you try focusing on the problem at hand and add to the solutions, and stop moaning and shit-stirring? It's freaking annoying.

    ReplyDelete
  14. Lars Fosdal Stefan may currently use XE and XE8 only in his day-to-day environment, so tested only for these versions.

    ReplyDelete
  15. Uwe Raabe - I just made a sigh of relief :)

    ReplyDelete
  16. If a bug exists in XE and XE8 I am confident to claim that the bug also exists in all versions in between without explicitly testing.

    ReplyDelete
  17. I expect that this bug is an ever present, stretching from the dawn of generics to the latest and greatest

    ReplyDelete
  18. +Paul "some kind of macro" is what we see as templates in C++, for example. They are incredibly powerful but have a cost of complexity. I envy the ability to write a library like Eigen, simply impossible Using generics.

    There are many reasons to have chosen generics over templates but likely the main one was for compatibility with .net. When generics were developed for Delphi, the Borland .net compiler was believed to be the future.

    ReplyDelete
  19. David Heffernan Aren't Delphi generics resolved at compile time? Now we have GetTypeKind which is like C++ std::is_same

    ReplyDelete
  20. José Ramírez Yes, generics are instantiated at compile time. Did anyone say otherwise?

    ReplyDelete
  21. David Heffernan I always thought they were instantiated at runtime..

    ReplyDelete

Post a Comment