ZetCode

Julia Enums Tutorial

last modified November 18, 2026

An enumeration, or enum, is a type whose values come from a fixed set of named constants. Instead of using raw integers such as 1 or 2 for the states of a program, an enum gives those values readable names and a distinct type. Julia creates enums with the @enum macro.

This tutorial shows how to define enums, list their instances, choose an underlying numeric type, convert values to integers and strings, iterate over them, and compare them. The type system they build on is described in Julia data types. All examples were verified with Julia 1.13.4.

Defining an Enum

The @enum macro creates a new type and a set of named values of that type. The names become globals in the current scope.

main.jl
@enum Fruit Apple Banana Cherry

println(Apple)
println(typeof(Apple))
println(instances(Fruit))

The first line defines the type Fruit with the three instances Apple, Banana, and Cherry. Printing an instance shows its name, and typeof reports the enum type. The instances function returns a tuple containing every value of the enum.

$ julia main.jl
Apple
Fruit
(Apple, Banana, Cherry)

Underlying Value Types

Each enum value is stored as an integer. By default the underlying type is Int32, but a different primitive type can be chosen after the enum name. The supertype function reveals which one was selected.

main.jl
@enum Color::UInt8 Red=1 Green=2 Blue=4

println(supertype(Color))
println(sizeof(Red), " ", sizeof(Blue))
println(Int(Red), " ", Int(Green), " ", Int(Blue))
println(typeof(Int(Red)))

Here Color uses UInt8, so each value occupies one byte and can hold numbers from 0 to 255. Explicit assignments give Red, Green, and Blue the values 1, 2, and 4; values must be unique. Converting an instance with Int returns a regular Int64 holding that number.

$ julia main.jl
Enum{UInt8}
1 1
1 2 4
Int64

Iterating Over Instances

Because instances returns a tuple, it can be used directly in a for loop. This is handy for displaying all the options of an enum or for building a lookup table.

main.jl
@enum Status Pending Active Done

for s in instances(Status)
    println(s, " = ", Int(s))
end

When no values are assigned explicitly, Julia numbers the instances starting at zero. The loop therefore prints Pending = 0, Active = 1, and Done = 2. Iteration is described more generally in Julia iterators.

$ julia main.jl
Pending = 0
Active = 1
Done = 2

Converting To and From Integers

Enum values can be converted to integers with Int and constructed from integers by calling the enum type as a function. An integer that is not one of the declared values raises an ArgumentError.

main.jl
@enum Fruit Apple Banana Cherry

println(Int(Apple), " ", Int(Banana), " ", Int(Cherry))
println(Fruit(1))
println(Fruit(2))
try
    println(Fruit(99))
catch e
    println(typeof(e))
end

The values are 0, 1, and 2, so Fruit(1) is Banana and Fruit(2) is Cherry. The call Fruit(99) has no matching instance and throws an ArgumentError, which the catch block reports. Conversion helpers are covered in Julia type conversion.

$ julia main.jl
0 1 2
Banana
Cherry
ArgumentError

Converting to Strings and Symbols

An enum value can be turned into text with string or repr, and into a Symbol with the Symbol constructor. These conversions are useful for output and for keys in text-based formats.

main.jl
@enum Fruit Apple Banana Cherry

println(string(Apple), " ", typeof(string(Apple)))
println(repr(Banana))
println(Symbol(Cherry), " ", typeof(Symbol(Cherry)))

Both string(Apple) and repr(Banana) give the instance's name as a String; for an enum value repr needs no quotation marks. Symbol(Cherry) produces the Symbol :Cherry, which prints without the leading colon. See Julia strings for more on text values.

$ julia main.jl
Apple String
Banana
Cherry Symbol

Comparing Enum Values

Enum values of the same type can be compared for equality and ordered by their underlying numbers. This means they can also be sorted.

main.jl
@enum Direction North=1 East=2 South=3 West=4

println(North == North, " ", North == South)
println(North < South)
println(sort([West, North, South, East]))

The first line shows that two references to the same instance are equal while different instances are not. Ordering follows the assigned values, so North < South is true and sorting returns the four directions from North to West. Sorting is covered in Julia arrays.

$ julia main.jl
true false
true
Direction[North, East, South, West]

Enums as Dictionary Keys

Enum values can be used as dictionary keys, which makes it easy to associate a value with each named state. Because findfirst works on the tuple returned by instances, the position of an instance can be recovered as well.

main.jl
@enum Fruit Apple Banana Cherry

prices = Dict{Fruit,Int}(Apple => 3, Banana => 2, Cherry => 5)
println(prices[Apple], " ", prices[Banana], " ", prices[Cherry])
println(findfirst(==(Cherry), instances(Fruit)))

The dictionary maps each fruit to a price: 3, 2, and 5. The second line finds the index of Cherry among the instances, which is 3 because Julia counts from one. Dictionaries are introduced in Julia dictionaries.

$ julia main.jl
3 2 5
3

Enums with Functions

Because an enum is a real type, it can be used in a function signature. The compiler then knows that the argument is one of the declared instances, which makes the code self-documenting and enables multiple dispatch.

main.jl
@enum Mode Read Write Execute

function describe(m::Mode)
    if m == Read
        return "reading"
    elseif m == Write
        return "writing"
    else
        return "executing"
    end
end

println(describe(Read), " ", describe(Write), " ", describe(Execute))

The describe function accepts only values of type Mode and returns a different string for each one. Passing an unrelated value would fail immediately with a method error. Multiple dispatch is explained in Julia multiple dispatch.

$ julia main.jl
reading writing executing

Best Practices for Enums

A few habits make enum-based code easier to maintain.

Source

Julia @enum documentation

Julia Types documentation

In this article we have worked with enums in Julia: defining them with @enum, underlying value types, iteration, conversion to integers and strings, comparison, dictionary keys, and use in function signatures. The type system is covered in Julia data types, dispatch in Julia multiple dispatch, and conversion in Julia type conversion.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.