Defining a struct
In Julia, the
struct keyword defines a new Composite Type, based on given field names, and optionally annotated individual types. By default, structs cannot be modified once initialized (i.e. are inmutable unless explicitly specified as mutable)
Let’s say we want to create a
Location type, containing a name, and
(lat,lon) coordinates. We can do this as follows:
struct Location name::String lat::Float32 lon::Float32 end
To initialize a struct with values, the default constructor is simply using the struct name as a function:
loc1 = Location("Los Angeles", 34.0522,-118.2437)
We can access the struct fields with the dot notation, as is common in several languages:
loc1.name # "Los Angeles" loc1.lat # 34.0522 loc1.lon # -118.2437
Having defined this Location struct, now enables us to do things such as defining a vector of Locations called
sites, and dynamically filling it with Location elements:
sites = Location push!(sites, Location("Los Angeles", 34.0522,-118.2437)) push!(sites, Location("Las Vegas", 36.1699,-115.1398))
Notice that a very similar result could have been obtained via Named Tuples. Introducing a new Type such as our Location type can be convenient for additional clarity.
If we want to be able to modify the components of a struct after it has been initialized, it suffices to declare the struct as mutable, as follows:
mutable struct mLocation name::String lat::Float32 lon::Float32 end
We can then do things like:
loc1 = mLocation("Los Angeles", 34.0522,-118.2437) loc1.name = "LA"
[email protected]: Defaults Values and Keyword-Based Constructors
[email protected] macro is a helpful tool that enables the use of default values in structs, and also keyword-based constructors.
For example, we can do the following:
[email protected] mutable struct Param Δt :: Float64 = 0.1 n :: Int64 m :: Int64 end P = Param(m=50, n=35)
Destructuring a Struct
In the above example we had a structure that worked as a simple placeholder for certain numerical parameters. While we can access our parameters by doing
P.n we might want to destructure the struct and create
n variables automatically.
In a similar fashion to NamedTuples, since Julia 1.7 we have the following syntax
(; n,Δt ) = P
This will get any field of
P that matches what we write on the left-hand side and turn it into a variable. Note that the order of the variables doesn’t matter, and that we don’t need to destructure all the fields.
print("The value of Δt is $(Δt)")
The value of Δt is 0.1