Equivalent of C# enum?

Does Julia have the equivalent of C#

            public enum AorB
            {
                A = 1,
                B = 2                
            }
AorB x = AorB.B

A variable that can take a fixed set of possible values.

enum

1 Like

More specifically, see @enum.

1 Like

You might be interested in EnumX.jl

1 Like

Thanks everyone. Very useful.

I notice EnumX objects can’t be used in type specification.
This gives an error

@enumx X a b

f( x::X  ) = x

As per the docs:

Fruit is a module – the actual enum type is defined as Fruit.T by default:

So you’ll need to do

julia> using EnumX

julia> @enumx Foo a b

julia> f(x::Foo.T) = x
f (generic function with 1 method)

julia> f(Foo.a)
Foo.a = 0

instead.

2 Likes

Thanks