Hope that you understand what I mean. I'm aware of the issue of copying or referencing the contents of the dynamic array. I'll test it myself. Please comment on writing the generic procedure.
Title: Re: How can I write a generic procedure?
Post by: bytebites on August 14, 2019, 05:18:39 am
Title: Re: How can I write a generic procedure?
Post by: Thaddy on August 14, 2019, 08:08:02 am
Note that such code is slow because of the copy semantics of SetLength(). So if you call this often....better use a TList<T> instead of an array. Lists grow more efficient. Conversion from array to list is easy. Create the list and call TList<T>.AddRange(values:array of T); Of course don't do that internally... but use lists instead of arrays. Only relevant if you add a lot.
Title: Re: How can I write a generic procedure?
Post by: PascalDragon on August 14, 2019, 09:26:13 am
Independently of that Thaddy's explanation that lists are more efficient if many items are added is correct: the example provided by bytebites is correct, however it requires FPC 3.2 or newer. You can alternatively put the procedure into a generic record as a class procedure.
In practice, I'd like to write a procedure that add new value to the end of a dynamic value.
If you want only this - don't reinvent the wheel. Use standard light TVector from fcl-stl package (C++ STL analog) or advanced TList from here - https://github.com/maciej-izak/generics.collections (clone and add to the project, if you are using FPC 3.0.4), it's Delphi compatible.
Title: Re: How can I write a generic procedure?
Post by: egsuh on August 15, 2019, 03:02:30 pm
Ah... There have been many possible solutions already. Each one is quite valuable. I appreciate all of your comments.