[fpc-pascal] Array of const and generics

Ryan Joseph ryan at thealchemistguild.com
Thu Mar 2 10:05:25 CET 2017


Is it possible to use array of const inside of generics?


Quick example I just typed up. If you specialize the generic the compiler always complains about the type in array of const not being the specialized type. Type casting doesn’t work either because you may have specialized for incompatible types like classes and strings. It would be nice to say: TIntegerCollection.Create([1, 3, 4, 8, 10]) as a way to init the instance.

type
  generic TCollection<T> = class (TObject)
    constructor Create (args: array of const);	
    procedure Add (a: T);
  end;
  TIntegerCollection = specialize TCollection<Integer>;


constructor TCollection.Create (args: array of const);
begin
  // ERROR: args[0] is not the type we specialized for. can’t typecast either.
  if args[0].vtype = vtinteger then
    Add(T(args[0].vinteger));
end;

procedure TCollection.Add (a: T);
begin
  
end;

Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list