# More comfortable enum?

**URL:** https://discourse.julialang.org/t/more-comfortable-enum/100779
**Category:** General Usage
**Tags:** question, enum
**Created:** [June 24, 2023, 2:12pm UTC](https://discourse.julialang.org/t/more-comfortable-enum/100779 "2023-06-24T14:12:36Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 24, 2023, 2:12pm UTC](https://discourse.julialang.org/t/more-comfortable-enum/100779/1 "2023-06-24T14:12:36Z")

</div>

What I would expect is, if I have an enum like:

```julia
@enum Color begin
    red
    green
    blue
end

```

That if I type `Color. <TAB><TAB>` that I get a list of colors to choose from.

But this is not the case.

Is there a way to achieve this behaviour?

---

<div class="post-metadata">

### Author: ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)
#### Post date: [June 24, 2023, 4:10pm UTC](https://discourse.julialang.org/t/more-comfortable-enum/100779/2 "2023-06-24T16:10:10Z")

</div>

[GitHub - fredrikekre/EnumX.jl: This is what I wish `Base.@enum` was.](https://github.com/fredrikekre/EnumX.jl) is one option.

I have also just added a module with some non-instantiable types for user-facing enums where I want users to discover the types through tab, for example (untested as Im typing on phone):

```julia
module Color
  abstract type red end
  abstract type green end
  abstract type blue end
end

```

Not sure if there are disadvantages with the latter though.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [June 24, 2023, 5:38pm UTC](https://discourse.julialang.org/t/more-comfortable-enum/100779/3 "2023-06-24T17:38:29Z")

</div>

> [@ufechner7](#):
>
> if I type `Color. <TAB><TAB>` that I get a list of colors to choose from

What you want is basically a namespace, so, in Julia, a `Module`. I usually simply write a small inline module when I want an enumeration type:

```julia
"""
`Actions.Action` is an enumeration type. The allowed values are:
1. `Actions.add` for adding
2. `Actions.move` for moving
3. `Actions.finish` for finishing
"""
module Actions
  struct UnknownActionException <: Exception
    unrecognized_action::Int
  end
  struct Action
    a::Int
    function Action(a::Int)
      (0 ≤ a ≤ 2) || throw(UnknownActionException(a))
      new(a)
    end
  end
  const add = Action(0)
  const move = Action(1)
  const finish = Action(2)
  const actions = (add, move, finish)
  const compile_time_test = (
    () ->
      if allunique(actions)
        nothing
      else
        error("inconsistent enumeration definition")
      end
  )()
end

```

Maybe it would make more sense to just use `EnumX`, though.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [June 24, 2023, 5:41pm UTC](https://discourse.julialang.org/t/more-comfortable-enum/100779/4 "2023-06-24T17:41:08Z")

</div>

> [@DrChainsaw](#):
>
> I have also just added a module with some non-instantiable types for user-facing enums where I want users to discover the types through tab, for example (untested as Im typing on phone):
> 
> ```julia
> module Color
> abstract type red end
> abstract type green end
> abstract type blue end
> end
> 
> ```
> 
> Not sure if there are disadvantages with the latter though.

Could you give more context on how do you use this? I suspect that using singleton `struct`s would be more idiomatic than using `abstract type`.

---

<div class="post-metadata">

### Author: ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)
#### Post date: [June 24, 2023, 8:08pm UTC](https://discourse.julialang.org/t/more-comfortable-enum/100779/5 "2023-06-24T20:08:48Z")

</div>

It was just for a very simple case when I wanted some labels for the user input, the kind where it is common to see symbols. I wanted them to be tabbable and I wanted to make sure everything discoverable through tab was a valid input.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 25, 2023, 6:33am UTC](https://discourse.julialang.org/t/more-comfortable-enum/100779/6 "2023-06-25T06:33:38Z")

</div>

EnumX looks like a solution (TAB completion works), but then I always need to prefix the enum values with the enum type… Not sure if I want this.

I have a function `run_tests(test...)` where test is a member of the enum TestCases. See:

```julia
function help()
    println("Run one or more of the following tests:\n")
    for test in instances(TestCases)
        println("run_tests($(String(Symbol(test))))")
    end
end

```

Not sure yet if I change anything, both enum and enumx have their advantages…

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [June 25, 2023, 12:58pm UTC](https://discourse.julialang.org/t/more-comfortable-enum/100779/7 "2023-06-25T12:58:47Z")

</div>

> [@ufechner7](#):
>
> but then I always need to prefix the enum values with the enum type… Not sure if I want this.

You can always define

```julia
julia> @enumx Fruit Apple Orange

julia> const Apple = Fruit.Apple

```

(which sort of defeats the purpose of the EnumX package, though).

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [June 25, 2023, 1:25pm UTC](https://discourse.julialang.org/t/more-comfortable-enum/100779/8 "2023-06-25T13:25:38Z")

</div>

Isn’t exactly this sort of problem addressed with module `import` and `using`?  
I would expect an `Enum` declaration to `export` values if it wishes, and then these would be in the namespace if the `Enum` is `using`-ed, and if I care about cluterring the namespace, then `import` can be used.

Can some sort of syntax/macro achieve this?

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 25, 2023, 6:15pm UTC](https://discourse.julialang.org/t/more-comfortable-enum/100779/9 "2023-06-25T18:15:56Z")

</div>

…

This is exactly what macro [`@enumx`](https://juliapackages.com/p/enumx) does. It do a simple Enum declaration inside a Module so it is immediately scoped.
