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;
type
TValues
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;
What happens if you change:
ReplyDeleteV:TIntegerValues;
to be
V:TValues;
?
the same
ReplyDeleteWhat version of Delphi?
ReplyDeleteCompiler bug, happens in XE and XE8. Report it and change your code as follows:
ReplyDeletetype
TValue = record
Key: string;
Value: T;
end;
TValues = array of TValue;
or
TValues = array of TPair;
or
TIntegerValues = TArray>;
Or like this
ReplyDeletetype
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
https://quality.embarcadero.com/browse/RSP-11388
ReplyDeleteWhile 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...and source code perfectly compiled by FPC. (Sarcasm) Nice, Embarcadero!
ReplyDeleteIs the problem present in 8.1 as well?
ReplyDeleteWhen I say XE8 that includes subscription update 1 of course :)
ReplyDeleteI fear that the upcoming migration to XE8.1 just came to a grinding halt.
ReplyDeleteLars Fosdal Why? This bug is an ever present.
ReplyDeleteit 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
ReplyDeletePaul TOTH Indeed. Over 15000 bugs are still waiting to be fixed by Embarcadero Technologies
ReplyDeleteDavid Heffernan - Did I misunderstand? I read Stefan saying it was in XE and XE8?
ReplyDeleteJosé 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.
ReplyDeleteLars Fosdal Stefan may currently use XE and XE8 only in his day-to-day environment, so tested only for these versions.
ReplyDeleteUwe Raabe - I just made a sigh of relief :)
ReplyDeleteIf 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.
ReplyDeleteI expect that this bug is an ever present, stretching from the dawn of generics to the latest and greatest
ReplyDeletehttp://18delphi.blogspot.ru/2015/02/containers-3-generics-and-without.html
ReplyDelete+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.
ReplyDeleteThere 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.
David Heffernan Aren't Delphi generics resolved at compile time? Now we have GetTypeKind which is like C++ std::is_same
ReplyDeleteJosé Ramírez Yes, generics are instantiated at compile time. Did anyone say otherwise?
ReplyDeleteDavid Heffernan I always thought they were instantiated at runtime..
ReplyDelete