Bug 10951 – EnumMembers should document about returning duplicate members
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-09-02T06:26:00Z
Last change time
2013-09-03T06:13:32Z
Keywords
pull
Assigned to
andrej.mitrovich
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2013-09-02T06:26:08Z
-----
import std.conv;
import std.stdio;
import std.string;
import std.traits;
import std.typetuple;
import std.socket;
void main()
{
string[] enums = to!(string[])([EnumMembers!SocketOption]);
writeln(enums.join("\n"));
}
-----
Prints:
DEBUG
BROADCAST
REUSEADDR
LINGER
OOBINLINE
SNDBUF
RCVBUF
DONTROUTE
SNDTIMEO
RCVTIMEO
ERROR
KEEPALIVE
ACCEPTCONN
RCVLOWAT
SNDLOWAT
TYPE
DEBUG -- note duplicate!
REUSEADDR -- note duplicate!
IPV6_MULTICAST_IF
IPV6_MULTICAST_LOOP
IPV6_MULTICAST_HOPS
IPV6_JOIN_GROUP
IPV6_LEAVE_GROUP
IPV6_V6ONLY
The workaround for user code is to use NoDuplicate from std.typetuple:
string[] enums = to!(string[])([NoDuplicates!(EnumMembers!SocketOption)]);
Comment #1 by monarchdodra — 2013-09-02T07:13:01Z
The root issue appears to be that enums can have identifiers with duplicate values:
//----
writefln("%(%d\n%)", [EnumMembers!SocketOption]);
//----
1
32
4
128
256
4097
4098
16
4101
4102
4103
8
2
4100
4099
4104
1 //HERE
4 //HERE
9
11
10
12
13
27
//----
So I'm not entirely sure EnumMembers is actually at fault here. It's the enum to string conversion that is "breaking". But at the same time, there are too enum members with the same value, so I'm not sure this is fixable. At best, documented and worked around.
Comment #2 by andrej.mitrovich — 2013-09-02T07:24:49Z
(In reply to comment #1)
> So I'm not entirely sure EnumMembers is actually at fault here. It's the enum
> to string conversion that is "breaking". But at the same time, there are too
> enum members with the same value, so I'm not sure this is fixable. At best,
> documented and worked around.
Hmm yeah, you're right. I think I'll just make a pull with a doc fix, by refering to NoDuplicates if someone wants to do code-generation with this trait.
Comment #3 by andrej.mitrovich — 2013-09-02T07:32:49Z