digitalmars.D.learn - How to iterate string enum values?
- Anton Pastukhov (20/20) Dec 23 2024 I'm stuck on a simple problem.
- bauss (9/29) Dec 23 2024 Simply cast el to a string instead of using std.conv.to, that way
- Anton Pastukhov (3/4) Dec 23 2024 Thanks much, it worked! Though I'm confused why. Could you please
- Jim Balter (17/21) Dec 23 2024 I had the exact same issue yesterday. Allow me to quote ChatGPT:
- Anton Pastukhov (5/6) Dec 24 2024 Thanks. That sounds plausible but I got burned by ChatGPT more
- monkyyy (6/14) Dec 24 2024 Id argue that the problem is that d's type system keeps track of
- IchorDev (7/11) Dec 28 2024 Well, `to!string` gets a *string representation* of a value. For
- Ogion (7/14) Dec 27 2024 Or use `std.conv.asOriginalType` to avoid implicit cast:
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 2024
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 2024
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 2024
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 2024
On Monday, 23 December 2024 at 23:46:33 UTC, Jim Balter wrote:I had the exact same issue yesterday. Allow me to quote ChatGPT:Thanks. That sounds plausible but I got burned by ChatGPT more than once, so I still would like to hear from a human being. Generally GhatGPT is not very good with D and hallucinates pretty often
Dec 24 2024
On Tuesday, 24 December 2024 at 12:07:56 UTC, Anton Pastukhov wrote:On Monday, 23 December 2024 at 23:46:33 UTC, Jim Balter wrote:Id argue that the problem is that d's type system keeps track of enum-ness in a strange way https://github.com/crazymonkyyy/debuglibprototype/blob/master/old/humanname.dI had the exact same issue yesterday. Allow me to quote ChatGPT:Thanks. That sounds plausible but I got burned by ChatGPT more than once, so I still would like to hear from a human being. Generally GhatGPT is not very good with D and hallucinates pretty oftenif(!is(T == enum))rapidly becomes nessery to spam everywhere
Dec 24 2024
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:Well, `to!string` gets a *string representation* of a value. For an enum, that's its member name. `cast(string)` just converts the enum value to its base type. Practically speaking, you can probably just use `[EnumMembers!BodyType]`, since `BodyType` will implicitly convert to its base-type of `string` anyway.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 28 2024
On Monday, 23 December 2024 at 20:26:47 UTC, bauss wrote: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; ```Or use `std.conv.asOriginalType` to avoid implicit cast: ```D auto values = [EnumMembers!BodyType] .map!asOriginalType .array; ```
Dec 27 2024