2

In Delphi I can initialize a constant array of non generic record like this:

type
  TMapEntry = record
    Key: Integer;
    Value: Integer;
  end;

const
  Arr: array[0..0] of TMapEntry = ((Key: 1; Value: 10));

But when I try to extent the code in a more generic way then I can't initialize the constant array:

type
  TMapEntry<KeyType, ValueType> = record
    Key: KeyType;
    Value: ValueType;
  end;

const
  Arr: array[0..0] of TMapEntry<Integer, Integer> = ((Key: 1; Value: 10)); //<-- Compile error

I even tried to use a type alias but got the same compile error:

type
  TIntMapEntry = TMapEntry<Integer, Integer>; 

const
  Arr: array[0..0] of TIntMapEntry = ((Key: 1; Value: 10)); //<-- Compile error

Is there any way to initialize a constant array of generic record?

PS: I am using Delphi 10.3

1 Answer 1

1

Is there any way to initialize a constant array of generic record?

No. Unfortunately this is a limitation of the compiler.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @David, if you claim that and didn't come up with a workaround it means there is no way so I accepted your answer as the correct one. But I'm curious: would not be easy for the compiler to solve this limitation? Could this be reported as a possible compiler improvement for future versions?
@Delmo I don't know the internals of the compiler. It's worth submitting a QP report, if there is not already one in existence. Then at least there is a record.
Just to state that "constant of generic record", like const cPair: TPair<byte, byte> = (Key: 1; Value: 1); is supported

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.