Bug of rtti??

Bug of rtti??
The first field cannot be serialized.

uses REST.Json;

type
TDateParamaters = record
Date: double;
IsShow: Boolean;
// Test: String; After add a string field, it works as expected
end;

TCurveParamaters = record
CurveCode: string;
CurveName: string;
end;

TTemplateInfo = class
private
m_dateArray: array [0 .. 4] of TDateParamaters;
m_curveArray: array [0 .. 4] of TCurveParamaters;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
LTemplate: TTemplateInfo;
begin
LTemplate := TTemplateInfo.Create;

// exptected:{"m_dateArray":[[0,false],[0,false],[0,false],[0,false],[0,false]],"m_curveArray":[["",""],["",""],["",""],["",""],["",""]]}
// actual:{"m_curveArray":[["",""],["",""],["",""],["",""],["",""]]}
Memo1.Text := TJSon.ObjectToJsonString(LTemplate);

LTemplate.Free;
end;

Comments

  1. Stefan Glienke - In your opinion - Is that a design flaw, or as it should be?
    Should we have parameterless ctors/dtors for records?

    ReplyDelete
  2. Lars Fosdal Let me quote Remy Lebeau from the linked thread:

    "This is a known issue, and is actually by design (whether or not that design is correct is another matter). RTTI stands for "Runtime *TYPE* Information", but Field2 is not using a previously defined type, it is using an anonymous type. instead Anonymous types do not generate RTTI."

    Now the reason for the other array to have RTTI is that for proper initialization/finalization of fields inside of a class the compiler needs to generate the RTTI - so we know that it is able to generate it for anonymous types. So as much as we argue - imo it's a design flaw. Every situation where you have such gotchas and surprises and then have to know why and all the technical details is bad (see https://en.wikipedia.org/wiki/Principle_of_least_astonishment)

    As for your other question:

    First lets see where this comes from - https://msdn.microsoft.com/de-de/library/aa288208(v=vs.71).aspx says:

    "Structs cannot contain explicit parameterless constructors. Struct members are automatically initialized to their default values."

    Now lets think back when those were introduced in Delphi - see a pattern with many other features from that time and their peculiarities?

    There are pros and cons especially when you think of struc... pardon records being initialized automatically - well for their managed members though (I heared we might get custom initializers and finalizers for records in some future Delphi version and we don't know how their syntax will be - personally I could live with constructor, destructor keywords because imo it fits).

    ReplyDelete

Post a Comment