[fpc-pascal] Record helpers on built in types?

Anthony Walter sysrpl at gmail.com
Wed Feb 18 22:10:54 CET 2015


I had a request. It would be nice if we could use class helpers with
generic types like so:

type
  TArray<T> = array of T;
  StringArray = TArray<string>;
  IntArray = TArray<Integer>;

type
  TArrayHelper<T> = record helper for TArray<T>
  private
    function GetLength: Integer;
    procedure SetLength(Value: Integer);
  public
    procedure Push(const Value: T);
    function Pop: T;
    property Length: Integer read GetLength write SetLength;
  end;

  StringArrayHelper = record helper(TArrayHelper<string>) for StringArray
  public
    function Join(const Separator: string): string;
  end;

  IntArrayHelper = record helper(TArrayHelper<Integer>) for IntArray
  public
    function Join(const Separator: string): string;
  end;

var
  Values: IntArray;
begin
  Values.Push(1);
  Values.Push(4);
  Values.Push(5);
  WriteLn(Values.Join(' | ')); // outputs '1 | 4 | 5'
end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150218/5f7e6a13/attachment.html>


More information about the fpc-pascal mailing list