Am 14.12.2016 08:09 schrieb "Lars" <nore...@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
_______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal