Why does the first enum member value default to zero instead of one?

I’m just curious since Julia uses 1-based indexing.

E.g.

julia> @enum Color Red Blue

julia> Red
Red::Color = 0

julia> Blue
Blue::Color = 1
2 Likes

Well, the underlying numbers of a @enum are not related to indexing (it cannot be used for it unless you explicitly convert it, or create a new getindex method), so there is no reason why not start at the first unsigned number.

You can define the starting value (or all values) of a @enum explicitly:

julia> @enum Color Red=1 Blue

julia> Red
Red::Color = 1

julia> Blue
Blue::Color = 2
1 Like

potentially because Enum might be used with Ccall to map some friend Enums

1 Like

This was already discussed: Why is @enum 0-based?

4 Likes