Feature request: Attribute with which to enable the requirement of explicit-initialization of enum variables
Jonathan M Davis
jmdavisProg at gmx.com
Sun Jun 2 20:03:00 PDT 2013
On Monday, June 03, 2013 04:57:28 Andrej Mitrovic wrote:
> On 6/3/13, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> > This is like how Java forces you to directly initialize things when it
> > doesn't think that you've initialized them elsewhere. D did _not_ take
> > that route. It when with have an init value for every type and
> > initializing with that.
> Yes, but we do have float.init which is NaN. We don't have the
> equivalent for enums.
The situation is exactly the same with ints. We don't have an actual error
value. 0 is the best we have. If you want something that's effectively invalid,
then creating an "invalid" value as the first enum value as you already
suggested would be the correct thing to do. Whereas what you're suggesting in
this thread would be the equivalent of making
int i;
illegal, only your suggestion is specifically for enums.
If programmers don't want to risk using the default value for one reason or
another, then they can choose to directly initialize it. I see no reason to
force that upon them, and it goes against how the rest of the language is
designed. Your suggestion for an "invalid" value for the first enum value was a
good one and should be enough IMHO if you don't want the default enum value to
be valid.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list