digitalmars.D.learn - How to iterate string enum values?
- Anton Pastukhov (20/20) Dec 23 I'm stuck on a simple problem.
- bauss (9/29) Dec 23 Simply cast el to a string instead of using std.conv.to, that way
- Anton Pastukhov (3/4) Dec 23 Thanks much, it worked! Though I'm confused why. Could you please
- Jim Balter (17/21) Dec 23 I had the exact same issue yesterday. Allow me to quote ChatGPT:
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
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 enlighteningSimply 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
On Monday, 23 December 2024 at 20:26:47 UTC, bauss wrote:Simply cast el to a string instead of using std.conv.toThanks much, it worked! Though I'm confused why. Could you please elaborate?
Dec 23
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: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.Simply cast el to a string instead of using std.conv.toThanks much, it worked! Though I'm confused why. Could you please elaborate?
Dec 23