std.algorithm.countUntil and alias

Salih Dincer salihdb at hotmail.com
Wed Oct 23 15:25:48 UTC 2024


On Wednesday, 23 October 2024 at 12:32:09 UTC, Anton Pastukhov 
wrote:
> I'm struggling with this code. Why `countUntil` won't work with 
> aliases?
> ```d
> import std.traits : EnumMembers;
> import std.algorithm : countUntil;
>
> enum Test: string
> {
>     One = "one",
>     Two = "two",
>     Three = "three"
> }
> ```

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!

```d
import std.traits : EnumMembers;
import std.algorithm : countUntil;

enum Test: string
{
     One = "one",
     Two = "two",
     Three = "three"
}

enum Numbers: size_t
{
     One = 1, Two, Three
}

struct MyStruct(T) {
   enum tMax = T.max;
   size_t idx;
}

alias myType = Numbers; // Test; // not works!
void main()
{
   alias myAlias = MyStruct!myType;

   auto rng = [EnumMembers!myType];
   auto idx = rng.countUntil!(e => e == myAlias.tMax);

   auto myStruct = myAlias(idx);
   assert(myStruct.idx == 2);
}
```
SDB at 79


More information about the Digitalmars-d-learn mailing list