MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Intermediate Julia

Lihat Kursus

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Create a mutable struct Employee
____ struct Employee
    name
    location
    age
end
Edit dan Jalankan Kode