3

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?

5
  • FWIW you can pass a constant array of string to a function expecting an "array of string" parameter. So something works! Commented Jul 4, 2013 at 1:52
  • 1
    possible duplicate of Is it possible do Declare a constant array that includes another constant array? Commented Jul 4, 2013 at 4:45
  • Why not simply use a variable? Commented Jul 4, 2013 at 7:17
  • This is similar to stackoverflow.com/questions/16247819/… as well Commented Jul 4, 2013 at 10:57
  • There's an ugly hack: include files. If each include file contains a list of string constants you can declare typed arrays with those in them: SILLY1 : array[1..3] of string = ({$i aa.inc}); (aa.inc contains a single line: 'one','two','three'). Might be justified if you have large collections of strings to manipulate. Commented Jul 4, 2013 at 22:09

1 Answer 1

4

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.

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

3 Comments

Any idea why typed constants are not constant expressions? I have always found this somewhat peculiar... Is this a matter of "just because" or is there some logic behind it that I'm missing?
@Marjan I'm sure there's logic, but only the original designers can answer with authority. In very early TP versions, typed constants were writeable. Perhaps that drove the design. I also find this constraint very limiting.
Ah, of course writeable constants. Another one of those contradictions... :)

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.