On Wednesday, 23 October 2024 at 15:25:48 UTC, Salih Dincer wrote:
If it were me, I would equip my type with aliases like below. But for some reason I don't understand, the enum Numbers works, while the enum Test which is of type string doesn't!


I figured out why it wasn't working. It turns out I had made a size_t comparison with a string. In this case, it's smart to move away from string altogether and use alias fun(S). Here's D's perfect syntax:

```d
struct Zoo(size_t i)
{
  size_t count;
  static index = i;
}

void main()
{
  enum Animal { dog = 3, cow, fox, cat }

  Animal[] animals;
  with(Animal) animals = [dog, cow, fox, cat];

  alias myType = Zoo!(Animal.fox);
  alias fun(S) = e => e == S.index;

  import std.algorithm : countUntil;
  auto myStruct = myType(
    animals.countUntil!(fun!myType)
  );

  assert(myStruct.index == 5);
  assert(myStruct.count == 2);

  import std.stdio;
  myStruct.writeln(": ", animals);
  // Zoo!5LU(2): [dog, cow, fox, cat]
}
```

SDB@79


Reply via email to