On Thursday, 11 April 2019 at 06:45:23 UTC, Basile B. wrote:
On Thursday, 11 April 2019 at 06:20:05 UTC, Julian wrote:
Is there a nicer way to have enum array keys in D?

No. I've myself written my own EnumIndexedArray [1] type. It's pretty simple. Just a couple of operator overload to preovide the syntax.

I went from ObjFPC/Delphi which has what you describe from Ada too and missed it. (typically: `enum TStuff = (); var stuffStrings: array[TStuff] of string;` ...)

[1] https://github.com/Basile-z/iz/blob/9ce6fc0e2e0c74f97d530ce598a6842b7b048f25/import/iz/enumset.d#L1086

Thanks. That still seems like enough work that I'd rather
do things the D way. At least if I don't also want Enum sets.

That gave me the idea for this though:

  import std.stdio;

  struct EnumRange(E) {
      int begin = E.min;
      int end = E.max + 1;
      bool empty() { return begin == end; }
      void popFront() { ++begin; }
      E front() { return cast(E) begin; }
  }

enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }

  void main() {
      int[Days.max+1] worklog;
      ++worklog[Days.Saturday];
      writeln("- worklog");
      EnumRange!(Days) why;
      foreach (day; why)
          writefln("%5d %s", worklog[day], day);
  }

Which I'm still disappointed is not:

   foreach (day; EnumRange!(Days))

Also, this isn't too bad:

  void main() {
      int[Days.max+1] worklog;
      ++worklog[Days.Saturday];
      writeln("- worklog");
      foreach (day, count; worklog)
          writefln("%5d %s", count, cast(Days) day);
  }

I don't see a difference in micro-benchmarks. *shrug*

Reply via email to