Re: [fpc-pascal] Generic routines for both dynamic array and other collections
> On Feb 20, 2021, at 7:52 PM, Виктор Матузенко via fpc-pascal > wrote: > > And how do I write generic helper for all possible dynamic arrays? By hand sadly. The RTL has added type helpers for many types but I don't think they added these for dynamic arrays. I agree that dynamic arrays should have a "Count" method by default and the RTL should provide "array of T" for all intrinsic types. Regards, Ryan Joseph ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Generic routines for both dynamic array and other collections
Am 21.02.2021 um 15:59 schrieb Ryan Joseph via fpc-pascal: On Feb 20, 2021, at 7:52 PM, Виктор Матузенко via fpc-pascal wrote: And how do I write generic helper for all possible dynamic arrays? By hand sadly. The RTL has added type helpers for many types but I don't think they added these for dynamic arrays. I agree that dynamic arrays should have a "Count" method by default and the RTL should provide "array of T" for all intrinsic types. You are supposed to use Length(X). There is no need for a "property". One doesn't need to objectify each and everything! Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Generic routines for both dynamic array and other collections
I've been using a lot of dynamic arrays of records lately, Some are even Dynamic Arrays of records that contain other records, for example: Type XYZ_Record = Record X,Y,Z : Double End; Call_Stack_Record = Record Location : LongInt; Offset : XYZ_Record; Rotation : Double; End; Call_Stack : Array of Call_Stack_Record; Then I do things like: Call_Stack[I].Offset.X := 12.345 Making a helper to handle "All Possible Dynamic Arrays" would have to handle this kind of dynamic array of record structure... then the helper would just end up being way more complicated than just using the dynamic array, because you can't know every kind of array that might be defined. I honestly don't see what you need a helper for dynamic arrays in the first place. They are pretty straight forward. I initialize (or reset) the array with SetLength(X,0); Then add things to it with: SetLength(X,Length(X)+1); X[Length(X)] := Data; Remove things from it with SetLength(X,Length(X)-1); Search through it with For I = 0 to Length(X)-1 Do Begin If X[I] = Test Then Do_Something; End; Etc... I had some count variables to keep track of how many elements were in the Arrays, but I got rid of them all because Length() is already there so I didn't see any reason to clutter up my code with another variable to keep track of... Length() ALWAYS reports an accurate count of the array elements... having a separate count variable could go wrong and be off from the actual number of elements. James -Original Message- From: fpc-pascal On Behalf Of Sven Barth via fpc-pascal Sent: Sunday, February 21, 2021 5:00 PM To: FPC-Pascal users discussions Cc: Sven Barth ; Ryan Joseph Subject: Re: [fpc-pascal] Generic routines for both dynamic array and other collections Am 21.02.2021 um 15:59 schrieb Ryan Joseph via fpc-pascal: > >> On Feb 20, 2021, at 7:52 PM, Виктор Матузенко via fpc-pascal >> wrote: >> >> And how do I write generic helper for all possible dynamic arrays? > By hand sadly. The RTL has added type helpers for many types but I don't > think they added these for dynamic arrays. I agree that dynamic arrays should > have a "Count" method by default and the RTL should provide "array of T" for > all intrinsic types. You are supposed to use Length(X). There is no need for a "property". One doesn't need to objectify each and everything! Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Generic routines for both dynamic array and other collections
> On Feb 21, 2021, at 2:59 PM, Sven Barth wrote: > > You are supposed to use Length(X). There is no need for a "property". One > doesn't need to objectify each and everything! Yes but programmers tend to be neurotic and obsessive. :) I think many of us would appreciate a matching "Count" method that is consistent with TList. Regards, Ryan Joseph ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Traits Proposal
> On Feb 19, 2021, at 8:50 AM, Ryan Joseph wrote: > > I just realized another potential problem. If we use the "default" keyword > that means there could be multiple "defaults" unless we limit the property to > 1 per class, which would kind of defeat the purpose of the feature (like the > issue we had with helpers and made multi-helpers for). Can you respond to this concern? It could be a deal breaker for the syntax. Regards, Ryan Joseph ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Generic routines for both dynamic array and other collections
James Richters schrieb am Mo., 22. Feb. 2021, 01:07: > I've been using a lot of dynamic arrays of records lately, Some are even > Dynamic Arrays of records that contain other records, for example: > Type >XYZ_Record = Record > X,Y,Z : Double >End; > >Call_Stack_Record = Record > Location : LongInt; > Offset : XYZ_Record; > Rotation : Double; >End; > >Call_Stack : Array of Call_Stack_Record; > > Then I do things like: > Call_Stack[I].Offset.X := 12.345 > > Making a helper to handle "All Possible Dynamic Arrays" would have to > handle this kind of dynamic array of record structure... > No. Assuming generic helpers would be supported it would be something like(!) this: === code begin === type generic TArrayHelper = type helper for array of T private function GetCount: SizeInt; inline; public property Count: SizeInt read GetCount; end; function TArrayHelper.GetCount: SizeInt; begin Result := Length(Self); end; type TLongintArrayHelper = specialize TArrayHelper; var arr: array of LongInt; begin SetLength(arr, 5); Writeln(arr.Count); end. === code end === In fact this is already possible for named array types. No special handling for your situation needed. And the generated code itself would be similar as well due to the inline. Doesn't change my opinion though that we don't *need* this. Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal