|
Registriert seit: 3. Sep 2023 455 Beiträge |
#1
Ich finde es zwar schon legitim, dass Delphi die Record-Felder nicht implizit initialisiert, ist ja eine Performance-Sache.
Ich finde es zwar schon legitim, dass Delphi die Record-Felder nicht implizit initialisiert, ist ja eine Performance-Sache.
Oder eben allein schon der Check als Compiler Hint oder gar Warning: "Hey, hier arbeitest Du mit nicht initialisierten Werten!", die Du dann beachten kannst oder aber im Stil von "Ich bin ein Software-Ingenieur, ich weiß, was ich tue", an der Stelle bewusst deaktivieren kannst, das wäre auch nett. Und ehrlich gesagt ist sowas inzwischen ja auch State-of-the-art. Where is the problem, it is in taking the whole thing in one way, one direction, and denying other approaches, i am always was (and still) with more tools and options to work and use and realize what in my head. Is there a way to have both without breaking backward compatibility, yes there is, but it needs open minds, sorry for the last phrase, because i already suggest similar or the same thing in other forum, and no one commented or did they, i can't remember. Anyway, how about adding new directive or modifier for the variables in the var section and for the inline variables, something like
Code:
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;
Procedure Test;
var MyRec: TMyRecord; Init; Another example , which i really wish we had in Pascal/Delphi:
Code:
No .Create, No .Free and definitely no that ugly nested try..finally, the compiler will insert the try..finally and call the constructor and destructor for each class, and we left with concrete will defined code, error free, and beautiful very beautiful and readable code, not breaking any thing.
procedure CompareAndAddList(TargetList, SrcList1, SrcList2 : TStringList);
begin ...... // irrelevant end; Procedure Test; var List1, List2, List3 :TStringList; Auto; begin List1.LoadFromFile.... List2.GetDataFromSomewhere.... CompareAndAddList(List1, List2, List3); end; In my opinion %99 of try..finally is just for calling Free, well some prefer here unnecessary FreeAndNil, but also doable. are these approaches viable ? , yes they are since the compiler does in fact insert try..finally automatically for managed types, then why it can't add it for any type, on other hand for non managed types, compiler as i understand has inline variables now, so this line "var i := 0 ;", well how is that hard to handle the variables in var sections as inline and but insert them after "begin" So technically it is there, just need to connect the dots, it is happening with all the VCL components, they are created and free without your code interaction, so have a public, private or protected var as TMyClass in Parent class declared as " CL: TMyClass; auto;" and the compiler will add create and free call in the parent constructor and destructor, how this is far away from using a string in that parent class ?!! Hope someone will read this and request such thing, so Embarcadero after finishing their cosmic ships (eg. LSP so we can live on Mars happily ever after), will have time to make some progress on the IDE, compiler, and may be the Lnaguage in this case. Sorry for ranting, and sorry more if this away from topic.
Kas
|
![]() |
Registriert seit: 6. Feb 2015 Ort: Stadtoldendorf 251 Beiträge Delphi 12 Athens |
#2
Wie ist es mit so einer Lösung? Spricht irgend etwas dagegen?
Delphi-Quellcode:
class operator TMyRecord.Initialize(out Dest: TMyRecord);
var LContext: TRttiContext; // Uses System.Rtti LType: TRttiType; LRecord: TRttiRecordType; LField: TRttiField; begin LContext := TRttiContext.Create; try LType := LContext.GetType(TypeInfo(TMyRecord)); if LType.IsRecord then begin LRecord := LType.AsRecord; //-- for LField in LRecord.GetFields do begin if (LField.FieldType.TypeKind=tkEnumeration) then begin if LField.FieldType.Handle = TypeInfo(Boolean) then LField.SetValue(@Dest, false) end else if (LField.FieldType.TypeKind=tkUString) then LField.SetValue(@Dest, '') else if (LField.FieldType.TypeKind=tkInteger) then LField.SetValue(@Dest, 0) else if (LField.FieldType.TypeKind=tkFloat) then LField.SetValue(@Dest, 0); end; end; finally LContext.Free; end; //Dest.Value := 10; //Dest := Default(TMyRecord); end; |
![]() |
Registriert seit: 11. Aug 2012 Ort: Essen 1.732 Beiträge Delphi 10.2 Tokyo Professional |
#3
Wie ist es mit so einer Lösung? Spricht irgend etwas dagegen?
Delphi-Quellcode:
if (LField.FieldType.TypeKind=tkUString) then
LField.SetValue(@Dest, '') else Ansonsten spricht nichts dagegen, allerdings fehlen viele weitere Datentypen, insbesondere Unter-Records. Und es funktioniert natürlich nur, wenn man die Erweiterte RTTI nicht abschaltet (wenn ich mich recht erinnere, ist das ein Compilerschalter).
Thomas Mueller
|
![]() |
Registriert seit: 8. Jan 2007 483 Beiträge |
#4
Code:
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;
Procedure Test;
var MyRec: TMyRecord; Init;
Delphi-Quellcode:
Or functions are outsourced to a record helper. Use it like this:
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;
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); ![]() ... No .Free and definitely no that ugly nested try..finally, ...
Delphi-Quellcode:
I would like to see the NameOf function much more urgently. Although it is highly voted, we hear nothing about its realization.
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; With best regards Thomas |
![]() |
Registriert seit: 3. Sep 2023 455 Beiträge |
#5
Code:
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;
Procedure Test;
var MyRec: TMyRecord; Init;
Delphi-Quellcode:
Or functions are outsourced to a record helper. Use it like this:
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;
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); ![]() ... No .Free and definitely no that ugly nested try..finally, ...
Delphi-Quellcode:
I would like to see the NameOf function much more urgently. Although it is highly voted, we hear nothing about its realization.
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; With best regards Thomas 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:
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.
type
Suit = (Club, Diamond, Heart, Spade); Size = (Small = 5, Medium = 10, Large = Small + Medium); SomeNumbers = -128..127; Caps = 'A'..'Z'; SomeNum: 1..500; 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
|
![]() |
Ansicht |
![]() |
![]() |
![]() |
ForumregelnEs ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.
BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus. Trackbacks are an
Pingbacks are an
Refbacks are aus
|
|
Nützliche Links |
Heutige Beiträge |
Sitemap |
Suchen |
Code-Library |
Wer ist online |
Alle Foren als gelesen markieren |
Gehe zu... |
LinkBack |
![]() |
![]() |