Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-03

Array of const and generics

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

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

Sven Barth
to Ryan Joseph
"Ryan Joseph" wrote 5 lines
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.

"array of const" itself should work without problems though mixing the type parameters and the values contained in the array might be problematic (I'd need to test it myself).
However for your specific example case why don't you use "array of T" as a parameter type for the constructor. It would work the same, but would be even more type safe and in the spirit of generics.

Regards,
Sven

Ryan Joseph
to Sven Barth
On Mar 2, 2017, at 5:45 PM, Sven Barth wrote: "array of const" itself should work without problems though mixing the type parameters and the values contained in the array might be problematic (I'd need to test it myself).

It doesn’t work from my tests.

However for your specific example case why don't you use "array of T" as a parameter type for the constructor. It would work the same, but would be even more type safe and in the spirit of generics.

I didn’t even know you could pass a dynamic array as a parameter so I was always using array of const. Thanks, learning more new things again!

Regards,
Ryan Joseph

Sven Barth
"Ryan Joseph" wrote
> On Mar 2, 2017, at 5:45 PM, Sven Barth
> > "array of const" itself should work without problems though mixing the

type parameters and the values contained in the array might be problematic (I'd need to test it myself).

It doesn’t work from my tests.

When I'll have the time I'll check whether it's correct that it doesn't work or not...

> However for your specific example case why don't you use "array of T"

as a parameter type for the constructor. It would work the same, but would be even more type safe and in the spirit of generics.

I didn’t even know you could pass a dynamic array as a parameter so I was

always using array of const. Thanks, learning more new things again!

It's not a dynamic array, but an open array, that's an important difference. You (currently) can't pass a an array constructor ("[...]") to a dynamic array parameter (that will change once we support such array constructors like Delphi does). Check the language reference guide for more details.

Regards,
Sven