Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to declare const array item values individually

Given the following enum:

type TEnum = (teA, teB, teC);

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.

like image 551
Jamie Kitson Avatar asked Jun 03 '15 15:06

Jamie Kitson


1 Answers

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
  );
like image 112
Rob Kennedy Avatar answered Nov 02 '22 05:11

Rob Kennedy