Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I would like to declare a const array of TEnum , however I find with the following the connection between the array items and enum items is relatively hard to read and maintain (obviously I am aware that I can comment verbosely and give each item its own line):

const AN_ARRAY : array[TEnum] of Integer = (1, 12, 146);

Is there a way to declare a const array more like this?

const
  AN_ARRAY : array[TEnum] of Integer : 
    AN_ARRAY[teA] = 1,
    AN_ARRAY[teB] = 12,
    AN_ARRAY[teC] = 146

Ideally I would like to set the enum ord values and not use the array at all, but this means that I then can't use TypeInfo to manipulate the enum.

What about {AN_ARRAY[teA] =} 1, ? :) I don't think that you can declare a constant array other way than an ordered list of values. – TLama Jun 3, 2015 at 16:01

No. The indices of an array constant are always implicit. Include them in comments if you need to see them beside their corresponding values, but beware that the comments may get out of sync with the real code, and the compiler won't warn you about that.

const
  AN_ARRAY : array[TEnum] of Integer = (
    1,  // teA
    12, // teB
    146 // teC
                Rob is not offering a solution to the problem. He is answering your question in the negative. There is no solution.
– David Heffernan
                Jun 3, 2015 at 16:41
                No, Jamie, there is indeed no guaranteeing the order. In this particular example, it's easy to recognize misordered entries because the names of the enum values have an obvious order (A, B, C). On the other hand, it would be hard to recognize a badly ordered list of entries corresponding to the values in TRuntimeError, for example.
– Rob Kennedy
                Jun 3, 2015 at 16:50
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.