enum

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Apr 10 00:19:18 PDT 2014


On 4/8/14, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
> There are several questions to ask ourselves.

There's another small issue with enums, you currently can't easily
create new aliases to enums where you want to define aliases for both
the enum name and the enum members.

For example:

-----
void take(cairo_status_t e) { }
cairo_status_t get() { return cairo_status_t.CAIRO_STATUS_SUCCESS; }

// C-style enum
enum cairo_status_t
{
    CAIRO_STATUS_SUCCESS,
    CAIRO_STATUS_NO_MEMORY,
}

// D-style enum
enum Status
{
    success = cairo_status_t.CAIRO_STATUS_SUCCESS,
    noMemory = cairo_status_t.CAIRO_STATUS_NO_MEMORY
}

void main()
{
    take(Status.success);    // ok, compiles
    Status x = get();   // error
}
-----

You could use a struct wrapper with some alias this trickery, but this
would be confusing compared to a normal enum, the documentation
wouldn't look right, and things like EnumMembers and other enum traits
would not work on this type.


More information about the Digitalmars-d mailing list