# Wanted: proper enum type and switch statement with pattern matching on enumerable types

**URL:** https://discourse.julialang.org/t/wanted-proper-enum-type-and-switch-statement-with-pattern-matching-on-enumerable-types/85225
**Category:** Internals & Design
**Tags:** enum
**Created:** [August 3, 2022, 12:23pm UTC](https://discourse.julialang.org/t/wanted-proper-enum-type-and-switch-statement-with-pattern-matching-on-enumerable-types/85225 "2022-08-03T12:23:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)
#### Post date: [August 3, 2022, 12:23pm UTC](https://discourse.julialang.org/t/wanted-proper-enum-type-and-switch-statement-with-pattern-matching-on-enumerable-types/85225/1 "2022-08-03T12:23:05Z")

</div>

I’ve returned to Julia development after a break of a couple of years and I was suprised that there still not a proper enum type or a switch (or match if you like Scala) statement.

The current enum with @enum is not good enough. Because the value names are not qualified/scoped with the type name it’s easy to get a name clash with variables,parameters, other enum values especially if you’re refactoring existing code and introducing enums. I’ve run into this myself. I’d like enum to be more like java enum where you say Type.value for a enum value (unless in some like a with statement where you then can leave of the Type)

I’d also like to see a switch/match statement for use with enums and more generally enumerable types like int, strings instead of long if/elseif/else chains. That would definitely cleanup some code that I have.  
Ideally the switch could be made more powerful with pattern matching like in Haskell or Scala. Even languages like Java/C# are introducing this now (or have done it). But I’d would already be happy with the basic case 🙂

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [August 3, 2022, 12:29pm UTC](https://discourse.julialang.org/t/wanted-proper-enum-type-and-switch-statement-with-pattern-matching-on-enumerable-types/85225/2 "2022-08-03T12:29:42Z")

</div>

Ref: [ANN: EnumX.jl -- improved enums for Julia](https://discourse.julialang.org/t/ann-enumx-jl-improved-enums-for-julia/78096)

Agree that `switch` would be nice, and better than `if`/`elseif ` chains, both visually and for correctness.

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [August 3, 2022, 7:13pm UTC](https://discourse.julialang.org/t/wanted-proper-enum-type-and-switch-statement-with-pattern-matching-on-enumerable-types/85225/3 "2022-08-03T19:13:01Z")

</div>

> [@Steven\_Sagaert](#):
>
> I’d also like to see a switch/match statement for use with enums and more generally enumerable types like int, strings instead of long if/elseif/else chains.

[https://thautwarm.github.io/MLStyle.jl/latest/syntax/pattern.html](https://thautwarm.github.io/MLStyle.jl/latest/syntax/pattern.html)

---

<div class="post-metadata">

### Author: ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)
#### Post date: [August 3, 2022, 9:07pm UTC](https://discourse.julialang.org/t/wanted-proper-enum-type-and-switch-statement-with-pattern-matching-on-enumerable-types/85225/4 "2022-08-03T21:07:13Z")

</div>

Switch can be emulated quite clearly due to short circuiting behaviour i.e

```julia
function fake_enum(x::Int)
           x == 1 && return 5
           x == 3 && return 2
           x == 5 && return 6
           x == 10 && return 12
           x == 3 && return 9
           x == 2 && return 1
       end

```

And LLVM can lower this to jump tables or other efficient ways of computing it

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [August 4, 2022, 8:04am UTC](https://discourse.julialang.org/t/wanted-proper-enum-type-and-switch-statement-with-pattern-matching-on-enumerable-types/85225/5 "2022-08-04T08:04:32Z")

</div>

There is an [open issue](https://github.com/JuliaLang/julia/issues/18285) with plenty of discussion regarding switch statement and pattern matching.

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [April 27, 2023, 9:57pm UTC](https://discourse.julialang.org/t/wanted-proper-enum-type-and-switch-statement-with-pattern-matching-on-enumerable-types/85225/6 "2023-04-27T21:57:08Z")

</div>

> **[GitHub - MasonProtter/SumTypes.jl: An implementation of Sum types in Julia](https://github.com/MasonProtter/SumTypes.jl)**
>
> An implementation of Sum types in Julia. Contribute to MasonProtter/SumTypes.jl development by creating an account on GitHub.
