# Cannot reuse enum member in different enum

**URL:** https://discourse.julialang.org/t/cannot-reuse-enum-member-in-different-enum/21342
**Category:** New to Julia
**Tags:** enum
**Created:** [March 1, 2019, 1:02pm UTC](https://discourse.julialang.org/t/cannot-reuse-enum-member-in-different-enum/21342 "2019-03-01T13:02:47Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ziong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ziong/32/7305_2.png) [@ziong](https://discourse.julialang.org/u/ziong)
#### Post date: [March 1, 2019, 1:02pm UTC](https://discourse.julialang.org/t/cannot-reuse-enum-member-in-different-enum/21342/1 "2019-03-01T13:02:47Z")

</div>

HI,

I have a question about defining enum. I noticed that enum member’s name cannot be reused in different enum, example as shown below:

```julia
julia> @enum FRUIT apple orange

julia> @enum COLOR red blue yellow orange
ERROR: invalid redefinition of constant orange
Stacktrace:
 [1] top-level scope at none:0

```

seems that all enum members will be registered as a constant in same scope, so that member cannot be repeated across all enum, is it an expected behaviour?

Actually, I think orange::COLOR and orange::FRUIT should be both valid.

Thanks.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 1, 2019, 1:07pm UTC](https://discourse.julialang.org/t/cannot-reuse-enum-member-in-different-enum/21342/2 "2019-03-01T13:07:30Z")

</div>

> [@ziong](#):
>
> is it an expected behaviour?

Yes, that’s the way enums work.

> [@ziong](#):
>
> I think orange::COLOR and orange::FRUIT should be both valid.

Generally, `x::T` and `x::S` cannot happen unless either `T <: S` or `S <: T`.

---

<div class="post-metadata">

### Author: ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)
#### Post date: [March 2, 2019, 3:08pm UTC](https://discourse.julialang.org/t/cannot-reuse-enum-member-in-different-enum/21342/3 "2019-03-02T15:08:46Z")

</div>

Check out [Encapsulating enum access via dot syntax - #4 by quinnj](https://discourse.julialang.org/t/encapsulating-enum-access-via-dot-syntax/11785/4) for a workaround (don’t miss the note at the bottom about Julia 1.0).

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [March 2, 2019, 6:07pm UTC](https://discourse.julialang.org/t/cannot-reuse-enum-member-in-different-enum/21342/4 "2019-03-02T18:07:04Z")

</div>

I rather wish we’d not shipped `@enum` in its current form in Julia 1.0—it’s really not great. Oh well. Better enum design is on my personal Julia 2.0 list.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [March 2, 2019, 7:21pm UTC](https://discourse.julialang.org/t/cannot-reuse-enum-member-in-different-enum/21342/5 "2019-03-02T19:21:28Z")

</div>

I often just create a tiny module containing only the enum, like [Encapsulating enum access via dot syntax - #2 by mbauman](https://discourse.julialang.org/t/encapsulating-enum-access-via-dot-syntax/11785/2).

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [March 2, 2019, 9:13pm UTC](https://discourse.julialang.org/t/cannot-reuse-enum-member-in-different-enum/21342/6 "2019-03-02T21:13:24Z")

</div>

The thing is that you want `MyEnum.name` to be the enum value named `name` and you want `MyEnum` to be the type as well. There’s also the tension between using symbols for this kind of thing versus enums. It rather seems that one wants an enum to be a limited kind of Symbol type with automatic domain checking and conversion to and from integer values for C interop.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 2, 2019, 9:43pm UTC](https://discourse.julialang.org/t/cannot-reuse-enum-member-in-different-enum/21342/7 "2019-03-02T21:43:29Z")

</div>

> [@StefanKarpinski](#):
>
> The thing is that you want `MyEnum.name` to be the enum value named `name` and you want `MyEnum` to be the type as well.

[Feature request: getproperty on enum type to access instances · Issue #31166 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/31166) talks about overloading `getproperty` for `Type{<:Enum}` which is interesting but might have problems since e.g. reflection likely uses getproperty on arbitrary types (similar to the problem of redefining show on your own types).

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [March 2, 2019, 10:38pm UTC](https://discourse.julialang.org/t/cannot-reuse-enum-member-in-different-enum/21342/8 "2019-03-02T22:38:35Z")

</div>

> [@StefanKarpinski](#):
>
> There’s also the tension between using symbols for this kind of thing versus enums. It rather seems that one wants an enum to be a limited kind of Symbol type with automatic domain checking and conversion to and from integer values for C interop.

We came to the conclusion to always use enums when applicable: [Style Guide · JuMP](http://www.juliaopt.org/JuMP.jl/v0.19.0/style/#@enum-vs.-Symbol-1)
