On Wednesday, 13 July 2016 at 12:54:19 UTC, ketmar wrote:
2OP: to create enum from string, you can use `std.conv.to`:
  enum E { A, B, C }
  E v = to!E("A");

I obviously failed to convey the purpose. I don't need a string representation of the enum member, I want to be able to attach UDAs to members. The second part, about Java-like enums, was just an thought. Scratch that.

Right now, I have this real code:

enum Severity: ubyte {
    INFO, WARNING, MINOR, MAJOR, CRITICAL
}
@notrace string severityToString(Severity sev) {
    final switch (sev) with (Severity) {
        case INFO:      return "INF";
        case WARNING:   return "WRN";
        case MINOR:     return "MNR";
        case MAJOR:     return "MJR";
        case CRITICAL:  return "CRT";
    }
}
@notrace Severity stringToSeverity(string sev) {
    switch (sev) with (Severity) {
        case "INF":  return INFO;
        case "WRN":  return WARNING;
        case "MNR":  return MINOR;
        case "MJR":  return MAJOR;
        case "CRT":  return CRITICAL;
        default:     return WARNING;
    }
}

If I could attach UDAs to the enum members', I could just auto-generate that stuff. That's all I'm asking for. The purpose of having a short name is for compression -- this text is going to be stored a lot, and I don't want to store the value, because (a) I don't care what value is assigned to each and (b) users might add new members, anywhere in the enum, causing the values to be remapped.

In other words, I don't want

enum Severity: ubyte {
    INFO, MINOR, MAJOR, WARNING, CRITICAL
}

to break the data I already have

-tomer

Reply via email to