Darian Miller 361 Posted November 13 In a codegen project I am working on, I am importing an API and creating the Delphi code to utilize it. I prefer working with records so most of the codegen is creating record types for the request/response objects exposed by the API. However, about 5% or less of this large API types involve circular references between types. I tried a few different approaches and couldn't come up with a decent solution with records so I starting to use forward declaration with classes, at least for this 5%. That seems to work, but now I think I need to make all the types classes instead of having a mixture. I ventured into Variants and even implemented my own custom type via TInvokeableVariantType to get some properties defined but generic variant custom record types don't work. It was a cool side trek but didn't solve this problem. After stewing on this for a while, I thought I'd ask for any feedback. Would you prefer a large API with 95% simple records and some occasional class-based request/responses due to a funky API design, or would you want to see it consistent throughout and only use classes? Or would you implement an alternative approach? TRecordA = record Field1:Integer; Field2:TRecordB; Field3:string; TopX:TRecordC; end; TRecordB = record Field4:Integer; Field5:TRecordA; end; TRecordC = record Items:TArray<TRecordA>; end; Share this post Link to post
PeaShooter_OMO 11 Posted November 13 (edited) @Darian Miller In times like these I just use pointers. Not sure whether that will affect the API interaction though. PRecordB = ^TRecordB; PRecordC = ^TRecordC; TRecordA = record Field1 : Integer; Field2 : PRecordB; Field3 : string; TopX : PRecordC; end; TRecordB = record Field4 : Integer; Field5 : TRecordA; end; TRecordC = record Items : TArray<TRecordA>; end; Edited November 13 by PeaShooter_OMO 1 Share this post Link to post
DelphiUdIT 176 Posted November 13 I disagree the use of pointers directly for a waste use. In the past I had some side effects not "beauty" ... especially if they were targeting structures with "managed types". In the state of Darian exposition, I will prefer the mix solution. CodGen projects are always "beasts", I wish you good luck in your job @Darian Miller 1 Share this post Link to post
Uwe Raabe 2057 Posted November 13 The TRecordA/TRecordB relation leads to a structure of infinite size. I would question the design choice, but having no knowledge of the underlying API that is not more than just a gut feeling. 7 hours ago, Darian Miller said: Would you prefer a large API with 95% simple records and some occasional class-based request/responses due to a funky API design, or would you want to see it consistent throughout and only use classes? IMHO, you should use the approach that fits best. There is no general rule for all cases. 1 Share this post Link to post