CommencerCommencer gratuitement

Mutable and typed structs

We've created our type Employee and have created an instance employee to store the details of our CEO. But some of the details will change over time - our CEO might change his name, his age will increase, and he might change his location. We want to keep up-to-date records of our employees, and so we need to be able to update the fields within our employee instance.

We also want to constrain the type of data that is filled out. When our data entry team enters the details of an employee, we want to safeguard against any accidental errors. We want to force:

  • the name to be a string
  • the age to be an integer value
  • the location to be a string

Recall that to create a mutable struct, we use the mutable keyword.

Cet exercice fait partie du cours

Intermediate Julia

Afficher le cours

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Create a mutable struct Employee
____ struct Employee
    name
    location
    age
end
Modifier et exécuter le code