[fpc-pascal] List of integers? Any class available in RTL or FCL to help?
Sven Barth
pascaldragon at googlemail.com
Wed Dec 14 11:12:45 CET 2016
Am 14.12.2016 08:09 schrieb "Lars" <noreply at z505.com>:
> A general purpose function that handles multiple types of arrays without
> rewriting the code for each array type looks like this:
>
> procedure AddItemToArray(
> const item: {$I T.inc}; var arr: {$I TArr.inc}); overload;
> var len: integer;
> begin
> // below is the same for EVERY type! Reuse, Reuse!
> len:= length(arr);
> setlength(arr, len+1);
> arr[len]:= item;
> end;
>
> Where $I T.inc defines the type being used.
Same code with 3.1.1's generics in non-Delphi mode:
=== code begin ===
generic procedure AddItemToArray<T>(
const item: T; var arr: specialize TArray<T>); overload;
var len: integer;
begin
// below is the same for EVERY type! Reuse, Reuse!
len:= length(arr);
setlength(arr, len+1);
arr[len]:= item;
end;
// used like this:
specialize AddItemToArray<Longint>(42, myarr);
=== code end ===
> Maybe I reinvented generics. Advantage of this, is no OOP (object
> orientation) required and it therefore works on any procedural code or OOP
> code, not just OOP code alone. One reason I dislike generics in modern
> languages is that it almost always requires object orientation to be used
> and you cannot program "generally" using procedural style coding. My
> include file parametric polymorphism allows procedural code to be written
> (such as dealing with simple arrays) generally.
FPC supports global generic routines (even in Delphi mode despite Delphi
itself not supporting it).
Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20161214/c22479a4/attachment.html>
More information about the fpc-pascal
mailing list