Is the order of enums guaranteed?

Hi,

is the order of enums guaranteed? E.g. if I have this enum:

const Phases = enum {
    onStartFrame,
    onUpdate,
    onRender,
};

can I rely on the idea that @intFromEnum(Phases.onStartFrame) will always be 0, and @intFromEnum(Phases.onUpdate) will always be 1?

(I found information about the order of unions and structs, which is not guaranteed unless they are marked as extern or packed. But for enums I found nothing.)

Thanks!

1 Like

Yes the order starts at 0 and counts up from there, you also can set values explicitly. See the comments in the code: Language Reference - enum

Interestingly it also shows that apparently it counts up from the previous entry, so setting a specific value influences the implicit ones after that.

Welcome to Ziggit!

5 Likes

Awesome! The most obvious place and I didn’t see it. :upside_down_face:

Thanks a lot, and thanks for the welcome! :slightly_smiling_face:

1 Like

What a fitting name to ask about enums :smile:

3 Likes

This made me curious about something:

const FunnyCount = enum(u8) {
    a = 3,
    b,
    c = 1,
    d,
    e, // ??
    f,
};

test "Funny Enum count" {
    try expectEqual(5, @intFromEnum(FunnyCount.e));
}

Spoiler:

error: enum tag value 3 already taken

5 Likes