Thema: Records

Einzelnen Beitrag anzeigen

Kas Ob.

Registriert seit: 3. Sep 2023
355 Beiträge
 
#27

AW: Records

  Alt 7. Nov 2023, 08:08
Code:
Procedure Test;
var
  MyRec: TMyRecord; Init;
and that it, also we can have a directive to tell the compiler to handle every record forward in this unit to Init to default, here comes simple initialization like filling with 0 or calling TMyRecord.Init;
In newer Delphi versions you can already do a lot. Defined with a few lines and clearer for me:
Delphi-Quellcode:
type
  TTestRec = record
    Name: String;
    Count: Integer;
    Address: array of record
      Street: String;
      ZIPCode: Integer;
    end;
  public
    function AddAddress(const pmcStreet: String; pmZIPCode: Integer): Integer;
    procedure Init;
    procedure Clear;
  end;

function TTestRec.AddAddress(const pmcStreet: String; pmZIPCode: Integer): Integer;
begin
  Result := Length(Address);
  SetLength(Address, Result + 1);
  with Address[Result] do
  begin
    Street := pmcStreet;
    ZIPCode := pmZIPCode;
  end;
end;

procedure TTestRec.Init;
begin
  FillChar(Self, SizeOf(Self), 0);
end;

procedure TTestRec.Clear;
begin
  Finalize(Self);
  Init;
end;
Or functions are outsourced to a record helper. Use it like this:
Delphi-Quellcode:
var
  rec: TTestRec;
begin
  rec.Init;
  ShowMessage(rec.Count.ToString);
  rec.AddAddress('Paddington', 123);
  ShowMessage(rec.Address[0].ZIPCode.ToString);
  rec.Clear;
  ShowMessage(Length(rec.Address).ToString);
Zitat von Kas Ob.:
... No .Free and definitely no that ugly nested try..finally, ...
There is something similar (not the same) in mORMot. Used very carefully, it can be helpful:
Delphi-Quellcode:
var
  sList1, sList2: TStringList;
  oList1, oList2: TObjectList;
begin
  with TAutoFree.One(sList1, TStringList.Create) do
  begin
    sList1.Add('Test');
    Another(oList1, TObjectList.Create);
    oList1.Add(TObject.Create);
  end;

  with TAutoFree.Several([
    @sList2, TStringList.Create,
    @oList2, TObjectList.Create]) do
  begin
    sList2.Add('Test');
    oList2.Add(TObject.Create);
  end;
end;
I would like to see the NameOf function much more urgently. Although it is highly voted, we hear nothing about its realization.

With best regards
Thomas
Nice points.

I am familiar with the above, so let me clear more on that.

1) Yes adding Init as record constructor to each and every record is doable, but, do i really want to add such code every where, or revert to helpers and lose the ability to use another helper in more constructive way for such small functionality, anyway it is not about record per se, see the suggested Init could be with anything like signed or unsigned integers, counters, index, float, chars, Booleans, short strings, array [fixedsize] of (bytes, chars, ), .... etc... and most critical my own types, simply for every type that can have value, if 0 is possible value then fill it, otherwise use the smallest value or the first defined like these, they could have default value and i don't need to worry about
Code:
type
  Suit = (Club, Diamond, Heart, Spade);
  Size = (Small = 5, Medium = 10, Large = Small + Medium);
  SomeNumbers = -128..127;
  Caps = 'A'..'Z';
  SomeNum: 1..500;
2) As for TAutoFree from mORMot, i did my share of those as i have my own solution, but still it is not elegant as if done by the compiler.. clear, beautiful and short, also my biggest problem where i count up to 1000 before decide to import and insert an open source library in my project for such small addition, such approach still and will stay error prone and the compiler will not help or warn about memory leak before hand, while such Auto functionality, i can rest assured it is done right.

3) NameOf and IndexOf and TypeOf .. OffsetOf, all of these worth adding not only for Pascal syntax and Delphi RTL but to the assembler too, also the ability to access VMT in easier and way with the ability to distinguish overridden method which will be great too, the point here, these methods are dangerous and defeat of secure coding by override the flow of code dictated by the language itself and curated by compiler, but we have crippled compiler and outdated language, so if Embarcadero can't/wouldn't revise and progress on the front, then we as users should have tools to overcome these ancient obstacles.
Kas
  Mit Zitat antworten Zitat