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

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 :slightly_smiling_face:

1 Like

Ref: ANN: EnumX.jl -- improved enums for Julia

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


1 Like

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

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

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

There is an open issue with plenty of discussion regarding switch statement and pattern matching.