The Julia Type System
Last updated on 2025-08-29 | Edit this page
Overview
Questions
- What is the use of types?
- How are types organized in Julia?
Objectives
- Understand the structure of the type tree.
- Know how to traverse the type tree.
- Know how to build mutable and immutable types.
Structuring variables
Melissa wants to keep the variables corresponding to the trebuchet
(counterweight, release_angle) separate from
the variables coming from the environment (wind,
target_distance). That is why she chooses to group them
together using structures. There are two structure types:
- immutable structures, whose fields can not be changed after creation
- keyword:
struct
- keyword:
- mutable structures, whose fields can change after creation
- keyword:
mutable struct
- keyword:
Since Melissa wants to change the parameters of the trebuchet, she
uses a mutable struct for it. But she cannot influence the
environment and thus uses a struct for those values.
Types and hierarchy
Here ::Float64 is a type specification, indicating that
this variable should be a 64-bit floating point number, and
:: is an operator that is read
“is an instance of.” If Melissa hadn’t specified the type, the variables
would have the type Any by default.
In Julia every type can have only one supertype, so let’s count how
many types are between Float64 and Any:
1.
OUTPUT
AbstractFloat
2.
OUTPUT
Real
3.
OUTPUT
Number
4.
OUTPUT
Any
So we have the relationship
Float64 <: AbstractFloat <: Real <: Number <: Any
where <:
is the subtype operator, used here to mean the item on the
left “is a subtype of” the item on the right.
Float64 is a concrete type, which means that
you can actually create objects of this type. For example
1.0 is an object of type Float64. We can check
this at the REPL using either (or both) the typeof function
or the isa
operator:
OUTPUT
Float64
or
OUTPUT
true
All the other types are abstract types that are used to
address groups of types. For example, if we declare a variable as
a::Real then it can be bound to any value that is a subtype
of Real.
Let’s quickly check what are all the subtypes of
Real:
OUTPUT
4-element Vector{Any}:
AbstractFloat
AbstractIrrational
Integer
Rational
This way the types form a tree with abstract types on the nodes and
concrete types as leaves. Have a look at this visualization of all
subtypes of Number: ![]()
The correct answer is 4: while 1 is an integer,
1.0 is a floating-point value.
Instances
So far Melissa only defined the layout of her new types
Trebuchet and Environment. To actually create
a value of this type she has to call the so called constructor,
which is a function with the same name as the corresponding type and as
many arguments as there are fields.
OUTPUT
Trebuchet(500.0, 0.7853981633974483)
Note how the values will get converted to the specified field type.
OUTPUT
Environment(5.0, 100.0)
trebuchet is called an instance or
object of the type Trebuchet. There can only ever
be one definition of the type Trebuchet but you can create
many instances of that type with different values for its fields.
Since the type Trebuchet was defined as a
mutable struct, the instance trebuchet can be
changed.
OUTPUT
1.2566370614359172
OUTPUT
Trebuchet(500.0, 1.2566370614359172)
The instance environment cannot, however, since the type
Environment is immutable.
ERROR
ERROR: setfield!: immutable struct of type Environment cannot be changed
Stacktrace:
[...]
A Little More about Types
Let’s look at an example of a parametric type:
Vector{T}. The braces indicate the parameter(s). The name
Vector without the parameter is a special type of abstract
type called a UnionAll. That’s because the parameter is
needed to specify a concrete type.
OUTPUT
UnionAll
OUTPUT
DataType
OUTPUT
(Vector, DenseVector, AbstractVector, Any)
OUTPUT
(Vector{Float64}, DenseVector{Float64}, AbstractVector{Float64}, Any)
Type constraints
You can see two “dimensions” of abstraction here, the hierarchy and the parameter. Remember abstract types represent “groups” of types that are intuitively similar. This is useful when defining structures or functions that need to be generic. When you need your function to apply to a group of possible input types, you simply think in terms of how tight the constraint needs to be.
Don’t change a variable’s type
While a variable that is specified to be an abstract type can change types within that constraint, it is best to avoid changing the type of a local variable, especially in places that are performance-critical.
Creating a subtype
A concrete type can be made a subtype of an abstract type with the
subtype operator <:. Since
Trebuchet contains several fields that are mutable Melissa
thinks it is a good idea to make it a subtype of
AbstractVector.
Caveat: Redefining Structs
JULIA
mutable struct Trebuchet <: AbstractVector{Float64}
counterweight::Float64
release_angle::Float64
end
ERROR
ERROR: invalid redefinition of constant Main.Trebuchet
Stacktrace:
[1] top-level scope
@ REPL[9]:1
This error message is clear: you’re not allowed to define a
struct using a name that’s already in use.
Restart the REPL
In Julia it is not very easy to redefine a struct. It is
necessary to restart the REPL to define the new definition of
Trebuchet, or take a different name instead.
Melissa decides to keep going and come back to this later.
- In Julia types have only one direct supertype.