[fpc-pascal] How can I implement "thrice" in Free Pascal?

Sven Barth pascaldragon at googlemail.com
Tue Oct 18 21:55:50 CEST 2011

On 18.10.2011 21:30, Jonas Maebe wrote:
> On 18 Oct 2011, at 20:03, Andrew Pennebaker wrote:
>> In particular, if anyone knows a way to implement a general concatenation
>> function Concat(Arr1, Arr2), let me know.
> I'm under the impression that you are trying to program in a statically typed language the same way as you'd use a dynamically typed language. Even with generic functions (which, as mentioned before, are not yet supported by FPC) you'd have to explicitly instantiate such a function for every type you'd want to do this for.

At least in theory it should work with generic functions (and using the 
Delphi compatible generic syntax):

=== source begin ===

   TGenArray<T> = array of T; // this should work in trunk already

function Concat<T>(Arr1, Arr2: TGenArray<T>): TGenArray<T>;
   SetLength(Result, Length(aArray1) + Length(aArray2));
   if Length(aArray1) > 0 then
     Move(aArray1[0], Result[0], Length(aArray1) * SizeOf(T));
   if Length(aArray2) > 0 then
     Move(aArray2[0], Result[Length(aArray1)], Length(aArray2) * SizeOf(T));

   arr1, arr2, res: array of Integer;
   // init arr1
   // init arr2
   res := Concat<Integer>(arr1, arr2);

=== source end ===

(tested using a non generic integer version)


More information about the fpc-pascal mailing list