# Why is @enum 0-based?

**URL:** <https://discourse.julialang.org/t/why-is-enum-0-based/12722>\
**Category:** General Usage\
**Tags:** enum\
**Created:** [July 29, 2018, 1:37pm UTC](https://discourse.julialang.org/t/why-is-enum-0-based/12722 "2018-07-29T13:37:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [July 29, 2018, 1:37pm UTC](https://discourse.julialang.org/t/why-is-enum-0-based/12722/1 "2018-07-29T13:37:04Z")

</div>

Arrays in Julia are 1-based, so why are Enums 0-based?

```julia
julia> Base.Enums.@enum ABC begin
         A
         B
         C
       end

julia> ABC(0)
A::ABC = 0

```

---

<div class="post-metadata">

**Author:** ![NaOH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naoh/32/5632_2.png) [@NaOH](https://discourse.julialang.org/u/NaOH)\
**Post date:** [July 29, 2018, 6:15pm UTC](https://discourse.julialang.org/t/why-is-enum-0-based/12722/2 "2018-07-29T18:15:17Z")

</div>

This was just recently discussed on Slack, in the gripes channel:

> **[Slack is your productivity platform](https://julialang.slack.com/?redir=%2Farchives%2FC67TK21LJ%2Fp1532367687000278%3Fname%3DC67TK21LJ%26perma%3D1532367687000278)**
>
> Slack is a new way to communicate with your team. It’s faster, better organized, and more secure than email.

> They’re partly designed for C interop which is probably why they’re like that - Stefan K.

I think they should start at 1 in a language like Julia (ie. using them as indexes), you could open an issue if you agree, or a PR, if you can change it yourself.

They will tell you that you can do:

`@enum RoundFoods Cookie=1 Cake Pizza Pie`

but that should be the default IMHO, and people using it for C interop, should be the one doing:

`@enum RoundFoods Cookie=0 Cake Pizza Pie`

Instead, as this is a generic language feature, not an interop specific feature.

It’s still breakage season, so why not? Better to have consistency across the language, good luck convincing them though.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [July 29, 2018, 8:48pm UTC](https://discourse.julialang.org/t/why-is-enum-0-based/12722/3 "2018-07-29T20:48:44Z")

</div>

> [@NaOH](#):
>
> > They’re partly designed for C interop which is probably why they’re like that - Stefan K.

I thought so.

I think it’s not too bad, because when you want to use them you usually don’t care about the actual values, and when you actually care about the values you probably want to specify them yourself.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [October 7, 2019, 11:28pm UTC](https://discourse.julialang.org/t/why-is-enum-0-based/12722/4 "2019-10-07T23:28:58Z")

</div>

I was about to ask the same question on here, and then I found this thread. I agree that enums should be one based. Sure, most of the time it doesn’t matter, and you can explicitly set the levels if you really want to, but just now I needed to create an array with my enums, like this,

```julia
julia> @enum Fruit apple orange kiwi

julia> Fruit.(0:2)
3-element Array{Fruit,1}:
 apple::Fruit = 0 
 orange::Fruit = 1
 kiwi::Fruit = 2

```

and it just hurt a little inside that I had to start my “indexing” at zero. It feels totally against the spirit of Julia’s (default) 1-based indexing.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 7, 2019, 11:49pm UTC](https://discourse.julialang.org/t/why-is-enum-0-based/12722/5 "2019-10-07T23:49:49Z")

</div>

> [@gdkrmr](#):
>
> Arrays in Julia are 1-based, so why are Enums 0-based?

Because enums aren’t arrays? They are a wrapper for a kind of integer type, and integer types on a computer have values that generally start at `0`, especially for unsigned types.

You might as well ask why `UInt8` values go from `0` to `255` and not `1` to `256`, or why `Bool` values are equivalent to `0` and `1` rather than `1` and `2`.

Indeed, you can define an `@enum` to be a wrapper around an unsigned integer or a boolean value, in which case starting the numbering at `1` by default would allow fewer possible values. For example, `@enum Switch::Bool begin Off; On; end` couldn’t work.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [October 8, 2019, 12:30am UTC](https://discourse.julialang.org/t/why-is-enum-0-based/12722/6 "2019-10-08T00:30:29Z")

</div>

Wow, it makes a lot more sense when you put it that way! The `@enum Switch::Bool begin On; Off; end` example really drives the point home. As an aside, I’ve read the docs for `@enum`, but I hadn’t quite realized that `@enum EnumName[::BaseType]` meant I could optionally annotate my enum type name, even though I know that’s how square brackets are used in other parts of the docs. So thanks on both accounts!

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [October 9, 2019, 8:50pm UTC](https://discourse.julialang.org/t/why-is-enum-0-based/12722/7 "2019-10-09T20:50:40Z")

</div>

I have to agree, this really makes sense now.
