D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10951 - EnumMembers should document about returning duplicate members
Summary: EnumMembers should document about returning duplicate members
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: Andrej Mitrovic
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2013-09-02 06:26 UTC by Andrej Mitrovic
Modified: 2013-09-03 06:13 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2013-09-02 06:26:08 UTC
-----
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 monarchdodra 2013-09-02 07:13:01 UTC
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 Andrej Mitrovic 2013-09-02 07:24:49 UTC
(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 4 monarchdodra 2013-09-03 03:44:04 UTC
I'm closing as "invalid", lest someone think, reading the title, that EnumMembers now doesn't return dupes.
Comment 5 Andrej Mitrovic 2013-09-03 06:13:32 UTC
I've renamed the title and set it as fixed. Otherwise it won't be in the changelog.