Question

I've got several constant arrays declared, and I'd like to declare more constants composed from those but I can't work out whether there's a reasonable way to do it.

const
  Common_Strings : array [0..1] of string = ('foo','bar');
  Extra_Strings : array [0..1] of string = ('some','extra');

What I'd like to do is one of these:

  Combined_Strings = Common_Strings + Extra_Strings;
  Combined_Strings = (Common_Strings, Extra_Strings);

This looks as though it should work, but is ugly to write and even uglier to maintain since my actual constants have 10+ elements:

  Combined_Strings = (Common_Strings[0], Common_Strings[1], Extra_Strings[0], Extra_Strings[1]);

But in a fit of stupidity, elements of a constant array are not necessarily constant: "[DCC Error] MyFile.pas(89): E2026 Constant expression expected". Obviously if I try to assign to them I get "[DCC Error] MyFile.pas(854): E2064 Left side cannot be assigned to".

I note that it's also not possible to write:

Duplicate_Constant = Common_Strings;

("Constant expression expected". Really.)

Is there a way to compose constant arrays into more constant arrays?

Was it helpful?

Solution

Typed constants can only be declared in terms of constant expressions. You are attempting to declare a typed constant in terms of typed constants. Since typed constants are not constant expressions, the compiler message that you see is by design.

The conclusion is that concatenation of two typed constant arrays can only be performed at runtime. And consequently the result of the concatenation can only be stored to a variable rather than a constant.

The documentation for array constants makes this clear (emphasis mine):

To declare an array constant, enclose the values of the array's elements, separated by commas, in parentheses at the end of the declaration. These values must be represented by constant expressions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top