Ambiguity in enum spec regarding discriminant choice

The reference says:

If there is no data attached to any of the variants of an enumeration, then the discriminant can be directly chosen and accessed.

These enumerations can be cast to integer types with the as operator by a numeric cast. The enumeration can optionally specify which integer each discriminant gets by following the variant name with = followed by a constant expression. If the first variant in the declaration is unspecified, then it is set to zero. For every other unspecified discriminant, it is set to one higher than the previous variant in the declaration.

In the community discord it came up that it's ambiguous whether this means that:

  • the first variant of a flat enum is always guaranteed to be zero (unless set)
  • the first variant of a flat enum is always guaranteed to be zero (unless set) if the = syntax is used at least once in the enum definition

Is this defined, and if so, which interpretation is correct?

The first (you don't need to use =).

While I agree this should be the definition, and I also agree this was the intention, the current text slightly leans toward the second interpretation so it needs to be fixed.

Personally, I thought the passage was relatively clear: the last three sentences all refer to whether each particular variant has a specified value. Perhaps a better fix would be to clarify the first of the three sentences:

For each variant in the enumeration, the corresponding integer can be optionally specified by following the variant name with = followed by a constant expression. If the first variant in the declaration is unspecified, then it is set to zero. For every other unspecified discriminant, it is set to one higher than the previous variant in the declaration.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.