Get startedGet started for free

Structs

1. Structs

Welcome to the final lesson in this chapter. We will cover constructors, one of the more complex concepts in Julia.

2. Structs - introduction

Our journey into structs starts with the composite type. A composite type is like any other type, however they can contain as many fields, or pieces of data as you want. To define a composite type, we use the struct keyword. When we apply this composite type like a function, we call it a constructor.

3. Structs - syntax

To create a composite type, we use the struct keyword. In this example, we have called our type Person, to represent some of the attributes of a person. We then place these attributes within the struct declaration. Here we have three things we want to store - the age, height, and location of a person. We now want to create an instance using this struct definition. We want to store the information on a person called Steve, so we call the struct and pass the values in as the arguments. We can now access the values within steve by using the dot notation. So to get steve's height, we call steve.height, and we can see that this returns 180, the height we passed in when creating the object.

4. Structs - typeof object

A quick way to reinforce the composite type background for a struct is by calling typeof on our new object steve. We can see that the type of our steve object is Person - reinforcing that Person is a composite type.

5. Structs - demonstrate immutability

By default, a struct is immutable. This means that once we create an object, we can not modify the values. As an example, recall our steve object, created from the Person struct. Steve has just had his birthday, so we want to increment his age by 1, from 18 to 19! Unfortunately, if we try to change steve's age, it is not possible. Immutable structs are the default in Julia, and so if we want to be able to make changes to these values within the steve object, we need to use a mutable struct.

6. Structs - mutable structs

A mutable struct is declared in the same fashion as a default struct, but instead we place the mutable keyword in front of the struct definition, like so. Now, if we try and change the age of our steve object from 18 to 19, we can see that it has changed and has not thrown an error. When defining a composite type, it is important to keep in mind the use case and whether or not you will need to change the values within your instance.

7. Structs - typed structs

You might have noticed that when we defined our struct, we could use any data type for the contents of the struct. For age, we could have passed a string containing the age, an integer, or a float. If you don't want to have this flexibility and want to protect the data type of the fields within your struct, you can use a typed struct. Let's change our Person class to only allow an integer value for both the age and the height of our person, and a string for the location. If we now tried to pass a float instead of an integer for the age for our instance steve, we get an error.

8. Let's practice!

You've covered a lot on one of the more complex topics in Julia. Let's go through some examples together.