[fpc-pascal] Feature announcement: Dynamic array extensions
Michael Van Canneyt
michael at freepascal.org
Fri May 25 08:06:00 CEST 2018
On Fri, 25 May 2018, Ryan Joseph wrote:
>
>
>> On May 25, 2018, at 12:43 PM, Michael Van Canneyt <michael at freepascal.org> wrote:
>>
>> I think the above is "right", and completely equivalent to sets, which are in some ways
>> like an array: a "collection" of same typed values.
>>
>> To add an element to a set you also do
>> MySet:=MySet+[aValue];
>>
>> That said, sets do have
>> Include(MySet,AValue);
>> to include a single value.
>>
>> Maybe this syntax can be extended to dynamic arrays.
>
> We can do + operator overloads on array classes though so it’s not totally crazy to see this syntax in code. Ultimately if it’s optimized well doing [xx] is no big problem I suppose.
>
> Having said that my feeling with dynamic arrays is that they’re more
> difficult to use than collection classes and lack basic functionality
> (until perhaps just now) so why use them? I just got burned really bad by
> some call to FPC_INTERLOCKEDINCREMENT64 from dynamic arrays which killed
> performance (who knows why) so I had to pull them out of all my code just
> in case.
Dynamic arrays are reference counted. The interlocked increment is there to
make the operations thread safe. I do not know whether the classes you speak
of are thread safe. If there is no FPC_INTERLOCKEDINCREMENT64, most likely
they are not.
Dynamic arrays have their reason for existence. For one thing, they are a continuous block
in memory, properly aligned and whatnot. The collection classes make no such
guarantees.
>
> The procedural syntax of Insert(arr, 0, value) is less enticing than arr.Insert(0, value) also but again we can fix that with type helpers.
Arrays are not objects. They do not have methods.
I understand where the tendency to make everything an object comes from
but the basic pascal types (including dynamic arrays) have always been handled
procedurally. Nothing wrong with that. I often write small programs
procedurally.
And that is why type helpers exist, to tack on an object-like approach.
Michael.
More information about the fpc-pascal
mailing list