Thread overview
How to iterate string enum values?
Dec 23
bauss
Dec 24
monkyyy
Dec 28
IchorDev
Dec 27
Ogion
December 23

I'm stuck on a simple problem.
There is this string enum of MIME types:

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:

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

December 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:

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:

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;
December 23

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?

December 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:

>

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.

December 24

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

December 24

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:

>

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

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.d

>

if(!is(T == enum))

rapidly becomes nessery to spam everywhere

December 27

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:

auto values = [EnumMembers!BodyType]
     .map!asOriginalType
     .array;
December 28

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?

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.