[Issue 24540] New: Add order/index to enum member to return its position
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon May 6 13:08:35 UTC 2024
https://issues.dlang.org/show_bug.cgi?id=24540
Issue ID: 24540
Summary: Add order/index to enum member to return its position
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: apz28 at hotmail.com
Currently to get the order/index of an enum member to be done in loop which is
slow. Provide such member will speedup the lookup
enum Foo
{
one=100, // First order/index=0
two=399, // Next is 1
...
}
auto checkingMember = Foo.two;
size_t index;
foreach (e; EnumMembers!Foo)
{
if (e == checkingMember)
return index;
index++;
}
One of the use case is provide a set of Foo using ubyte/ushort/uint/ulong
struct Set(E)
{
void set(Foo e)
{
v |= 1 << e.order;
}
bool isSet(Foo e)
{
return (v & (1 << e.order)) != 0;
}
More...functions
uint v;
}
--
More information about the Digitalmars-d-bugs
mailing list