# The length of a enum object in julia?

**URL:** <https://discourse.julialang.org/t/the-length-of-a-enum-object-in-julia/19865>\
**Category:** General Usage\
**Tags:** enum\
**Created:** [January 21, 2019, 10:03am UTC](https://discourse.julialang.org/t/the-length-of-a-enum-object-in-julia/19865 "2019-01-21T10:03:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [January 21, 2019, 10:03am UTC](https://discourse.julialang.org/t/the-length-of-a-enum-object-in-julia/19865/1 "2019-01-21T10:03:03Z")

</div>

I was wondering, is there a quick julia way to get the length of an enum type? For example, something like  
`@enum P n h m l `  
and then `len(P)` or `length(P)` would return 3?

**EDIT:** one more side note, I do not quite understand why did this enum type start counting from 0, not from 1? I thought Julia is more matlab/math friendly, by that I mean it usually gets counting from 1, not 0, right?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 21, 2019, 10:16am UTC](https://discourse.julialang.org/t/the-length-of-a-enum-object-in-julia/19865/2 "2019-01-21T10:16:14Z")

</div>

This might be helpful?

```julia
julia> @enum P n h m l

julia> P
Enum P:
n = 0
h = 1
m = 2
l = 3

julia> P.size
4

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 21, 2019, 10:29am UTC](https://discourse.julialang.org/t/the-length-of-a-enum-object-in-julia/19865/3 "2019-01-21T10:29:12Z")

</div>

No, I don’t think so:

```julia
julia> @enum P one two; P.size
4

julia> @enum P one two three four five; P.size
4

```

This is the size of the type in bytes, I think. This works:

```julia
julia> instances(P)
(one::P = 0, two::P = 1, three::P = 2, four::P = 3, five::P = 4)

julia> length(instances(P))
5

```

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [January 21, 2019, 10:41am UTC](https://discourse.julialang.org/t/the-length-of-a-enum-object-in-julia/19865/4 "2019-01-21T10:41:41Z")

</div>

@mauro3, I tested it. It looks that instances(P) would return a tuple of all the enumerated elements. And length(instances(P)) would return the length of that tuple, which is the length needed.  
Thank you both so much for the clarification. This makes Julia learning a much better experience. @nilshg, @mauro3
