[fpc-pascal] SetLength procedure

leledumbo leledumbo_cool at yahoo.co.id
Sun Jul 27 13:51:26 CEST 2014

> It is only a personal preference, not a serious problem. 
> I would find code in the form of 
>    ArrayLen := IncLength(ArrayName, 1); 
> to be much more readable then 
>     SetLength(ArrayName, Length(ArrayName)+1); 
>     ArrayLen:= High(ArrayName); 
> I was just hoping somebody may know how its done. 

Without support for generic function, currently you have to use generic
class method:

  generic TDynArrayUtil<T> = class
    class function SetLength(var a: T; const n: Integer): Integer;

class function TDynArrayUtil.SetLength(var a: T; const n: Integer): Integer;
  System.SetLength(a,Length(a) + n);
  Result := High(a);

and use it like:

  a: TIntegerDynArray;
maxn := specialize TDynArrayUtil<TIntegerDynArray>.SetLength(a,1);

note that you have to instantiate with the correct type.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/SetLength-procedure-tp5719807p5719810.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.

More information about the fpc-pascal mailing list