ZetCode

Julia Structs Tutorial

last modified November 18, 2026

A struct, or composite type, is a custom data type that groups named values called fields under one name. Structs are the main way to model data in Julia.

Julia distinguishes immutable struct from mutable struct, and allows both inner and outer constructors as well as parametric definitions. This tutorial covers all of them; the examples were verified with Julia 1.13.4.

Defining a Struct

A struct is declared with the struct keyword, a name, and a list of fields. Each field has a name and a type.

main.jl
struct Point
    x::Int
    y::Int
end
p = Point(3, 4)
println(p)
println(p.x, " ", p.y)
println(typeof(p))

The definition describes the shape of the data: a Point has two Int fields. Julia automatically provides a constructor that takes one value per field, so Point(3, 4) creates an instance. Fields are read with the dot syntax, and printing the object shows its type and field values.

$ julia main.jl
Point(3, 4)
3 4
Point

Structs Are Immutable

By default a struct is immutable: once it is created, its fields cannot be changed.

main.jl
struct Point
    x::Int
    y::Int
end
p = Point(3, 4)
try
    p.x = 10
catch e
    println(typeof(e))
end
println(p.x)

Assigning to p.x raises an ErrorException, which we catch here to keep the program running. The original value is unchanged. Immutability helps the compiler: an immutable object can be stored directly in arrays and passed around without defensive copying.

$ julia main.jl
ErrorException
3

Mutable Structs

When a type really needs to change after construction, declare it with mutable struct.

main.jl
mutable struct Point
    x::Int
    y::Int
end
p = Point(3, 4)
p.x = 10
println(p)
println(p.x)

The only difference in the definition is the mutable keyword, but now the field assignment succeeds and the printed object reflects the new value. Mutability has a cost, so prefer an immutable struct unless modification is genuinely needed.

$ julia main.jl
Point(10, 4)
10

Inspecting Fields

Julia can report the structure of a type without creating an instance.

main.jl
struct Point
    x::Int
    y::Float64
end
println(fieldnames(Point))
println(fieldtypes(Point))
println(fieldcount(Point))

fieldnames returns the names as a tuple of symbols, fieldtypes returns their types as a tuple, and fieldcount returns how many fields there are. These reflection functions are handy in generic code that works with many different structs.

$ julia main.jl
(:x, :y)
(Int64, Float64)
2

Inner Constructors

An inner constructor is defined inside the struct body. It has access to the special new function, which creates the object.

main.jl
struct Point
    x::Int
    y::Int
    Point(x::Int, y::Int) = new(x, y)
end
println(Point(1, 2))

Defining this constructor replaces the default one. The arguments are already typed as Int, and new fills the fields in declaration order. Inner constructors are the place to enforce invariants — conditions that must hold for every valid object.

$ julia main.jl
Point(1, 2)

Validating in an Inner Constructor

An inner constructor can reject invalid input before an object is created.

main.jl
struct Point
    x::Int
    y::Int
    function Point(x::Int, y::Int)
        if x < 0 || y < 0
            error("coordinates must be non-negative")
        end
        new(x, y)
    end
end
println(Point(1, 2))
try
    Point(-1, 5)
catch e
    println(typeof(e), ": ", e)
end

The long form uses the function keyword. A negative coordinate causes error to raise an exception, so no invalid Point can exist. This is the main reason to write an inner constructor instead of relying on the default one.

$ julia main.jl
Point(1, 2)
ErrorException: ErrorException("coordinates must be non-negative")

Outer Constructors

An outer constructor is an ordinary method defined outside the struct. It is used to provide convenience forms that delegate to an existing constructor.

main.jl
struct Point
    x::Int
    y::Int
end
Point(x, y) = Point(Int(x), Int(y))
Point() = Point(0, 0)
println(Point(3.0, 4.0))
println(Point())

The first outer method converts its arguments to Int and calls the default constructor; the second supplies a zero-argument form. Outer constructors cannot use new, but they can add as many convenient signatures as needed.

$ julia main.jl
Point(3, 4)
Point(0, 0)

Parametric Types

A parametric struct leaves one or more types as parameters, filled in at construction time. This keeps the definition generic while preserving type information.

main.jl
struct Point{T}
    x::T
    y::T
end
p1 = Point(1, 2)
p2 = Point(1.5, 2.5)
println(typeof(p1))
println(typeof(p2))
println(p1 isa Point{Int})

Julia infers T from the arguments: p1 is a Point{Int64} and p2 is a Point{Float64}. Both fields of a given instance share the same type. The check p1 isa Point{Int} confirms that the parameter is part of the type identity.

$ julia main.jl
Point{Int64}
Point{Float64}
true

Constrained Parameters

A parameter can be limited to a family of types, so that only suitable arguments are accepted.

main.jl
struct Point{T<:Real}
    x::T
    y::T
end
println(Point(3, 4))
println(Point(3.5, 4.5))
try
    Point("a", "b")
catch e
    println(typeof(e))
end

The annotation T<:Real means the parameter must be a subtype of Real. Integer and float points are created successfully, while strings produce a MethodError because String is not a real number.

$ julia main.jl
Point{Int64}(3, 4)
Point{Float64}(3.5, 4.5)
MethodError

Multiple Type Parameters

A struct may carry more than one independent parameter.

main.jl
struct Pair{A,B}
    first::A
    second::B
end
p = Pair("answer", 42)
println(typeof(p))
println(p.first, " = ", p.second)

Here the two fields may have different types. Julia reports the concrete type as Pair{String, Int64}, and each field keeps its own type. This is the pattern behind many container types in Julia's standard library.

$ julia main.jl
Pair{String, Int64}
answer = 42

Best Practices for Structs

A few rules of thumb help when designing composite types.

Source

Julia Types documentation

Julia Composite Types documentation

In this article we have defined Julia structs: immutable and mutable composite types, their fields, inner and outer constructors, and parametric types. Field types originate from the built-in types described in Julia data types, and structs can be placed in an abstract type hierarchy. To add behaviour to these types, see Julia multiple dispatch and Julia interfaces.

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.