www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to iterate string enum values?

reply Anton Pastukhov <mail anton9.com> writes:
I'm stuck on a simple problem.
There is this string enum of MIME types:

```d
enum BodyType: string {
     PlainText = "text/plain",
     JSON = "apllication/json",
     FormUrlencoded = "application/x-www-form-urlencoded",
     Multipart = "multipart/form-data",
     Other = "Other",
     None = "None"
}
```

Q: how can I iterate its values? With keys it's relatively easy:

```d
auto keys = [EnumMembers!BodyType]
     .map!(el => to!string(el))
     .array;

```
With values, though, I'm kinda stuck. Reading Ali's book and 
https://dlang.org/spec/enum.html did not bring enlightening
Dec 23
parent reply bauss <jacobbauss gmail.com> writes:
On Monday, 23 December 2024 at 20:20:02 UTC, Anton Pastukhov 
wrote:
 I'm stuck on a simple problem.
 There is this string enum of MIME types:

 ```d
 enum BodyType: string {
     PlainText = "text/plain",
     JSON = "apllication/json",
     FormUrlencoded = "application/x-www-form-urlencoded",
     Multipart = "multipart/form-data",
     Other = "Other",
     None = "None"
 }
 ```

 Q: how can I iterate its values? With keys it's relatively easy:

 ```d
 auto keys = [EnumMembers!BodyType]
     .map!(el => to!string(el))
     .array;

 ```
 With values, though, I'm kinda stuck. Reading Ali's book and 
 https://dlang.org/spec/enum.html did not bring enlightening
Simply cast el to a string instead of using std.conv.to, that way you retrieve the values. ``` auto values = [EnumMembers!BodyType] .map!(el => cast(string)el) .array; ```
Dec 23
parent reply Anton Pastukhov <mail anton9.com> writes:
On Monday, 23 December 2024 at 20:26:47 UTC, bauss wrote:

 Simply cast el to a string instead of using std.conv.to
Thanks much, it worked! Though I'm confused why. Could you please elaborate?
Dec 23
parent Jim Balter <jqbalter gmail.com> writes:
On Monday, 23 December 2024 at 20:32:58 UTC, Anton Pastukhov 
wrote:
 On Monday, 23 December 2024 at 20:26:47 UTC, bauss wrote:

 Simply cast el to a string instead of using std.conv.to
Thanks much, it worked! Though I'm confused why. Could you please elaborate?
I had the exact same issue yesterday. Allow me to quote ChatGPT: You're correct that .to!string also produces the member name rather than the value of the enum, which can be confusing. This behavior occurs because .to!string calls the toString function defined for the enum type, which defaults to returning the name of the enum member rather than its associated value. Why .to!string behaves this way: Enums in D are designed to emphasize their symbolic names over their values. The toString implementation for enums is tailored to reflect this by default, focusing on member names instead of their underlying values. The std.conv.to function, when applied to an enum, invokes this toString behavior. Correct way to get the value: If you want to convert the underlying value of the enum member to a string, you must explicitly cast the enum to its base type.
Dec 23