Walter Bright:

Thank you for the answers.

Here's one:

  enum Index { A, B, C }
  T[Index.max] array; // Error: Index.max is not an int
  array[B] = t;   // Error: B is not an int

In the last months I've grown a moderate desire for optionally strongly typed array indexes in D (as seen in Ada, but with a different syntax) (it's optional, so it's meant to be an additive change, that causes no harm to existing D code). With them code like yours becomes OK (as it's OK in Ada). Such optional strong typing for array indexes is not means for script-like D programs, but for the medium-integrity D programs.

And another:

  array[A + 1] = t; // Error: incompatible types Index and int

This can be solved with optionally strongly typed array indexes plus a succ/prec property for enums. I have asked for such property years ago. In Ada you use the built in function "Succ". Alternatively, in D you can also use a library-defined group of little functions/templates succ/prec/Succ/Prec (that contain a cast, but it's in Phobos, so it's less dangerous than a cast in user code):

array[Succ!(Index.A)] = t;
auto i = Index.A;
array[i.succ] = t;

And another:

  enum Mask { A=1,B=4 }

  Mask m = A | B;   // Error: incompatible operator | for enum

In GitHub there is a patch that is meant to implement Flags in library code (in C# such Flags are almost first-class, using [Flags]):

If such Flags is implemented with enums, then it contains casts, but again casts in Phobos are less dangerous than casts in user code.


Reply via email to