Feature request: Attribute with which to enable the requirement of explicit-initialization of enum variables

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Jun 3 06:47:09 PDT 2013


On Sun, 02 Jun 2013 20:03 -0700, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> 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.

Actually, I just figured out that we already have support for what
I've asked for:

enum_mod.d:
-----
module enum_mod;

private enum MachineEnum
{
    X86,
    X86_64,
}

struct InitEnum(E) if (is(E == enum))
{
    @disable this();
    this(E e) { value = e; }

    E value;
    alias value this;
}

alias Machine = InitEnum!MachineEnum;  // "fake" enum
-----

test.d:
-----
import enum_mod;

void main()
{
    // Machine machine;  // compile-time error
    Machine machine = Machine.X86;  // ok
}
-----

How damn cool is that?


More information about the Digitalmars-d mailing list