ProgramGuru

Structs and value semantics

Structs and value semantics

A student record belongs together: a name, a score and operations on that record. A struct defines a new value type containing properties and methods. Assigning a struct produces another logical value, so changing a copy does not change the original record.

What you’ll learn

  • Trace a struct copy and a mutating method.
  • Distinguish wrapper value semantics from reference-typed members.

Structs and value semantics in the example

  1. Original record

    A student record belongs together: a name, a score and operations on that record.

    Original record
  2. Copy the value

    Properties describe stored data.

    Copy the value
  3. Change only the copy

    The original score stays 70, while copy becomes 75.

    Change only the copy
The original score stays 70, while copy becomes 75. The method changes only the mutable instance on which it was called. This observable independence is value semantics, regardless of how storage is optimized internally.

How structs and value semantics works

Properties describe stored data. Methods are functions attached to the type. A method that changes a struct’s own stored state must be marked mutating, and the instance being changed must be mutable. A simple struct receives a memberwise initializer when its declarations allow it.

Work through structs and value semantics

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

struct Student {
  let name: String
  var score: Int
  mutating func addBonus() { score += 5 }
}
let original = Student(name: "Diya", score: 70)
var copy = original
copy.addBonus()
print(original.score, copy.score)

Expected output

70 75

Follow the execution

The original score stays 70, while copy becomes 75. The method changes only the mutable instance on which it was called. This observable independence is value semantics, regardless of how storage is optimized internally.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Structs and value semantics

Your task: Can original.addBonus() compile while original is let?

Swift · complete program

struct Student {
  let name: String
  var score: Int
  mutating func addBonus() { score += 5 }
}
let original = Student(name: "Diya", score: 70)
var copy = original
copy.addBonus()
print(original.score, copy.score)

Worked answer

No. The mutating method changes the struct value, and a let-bound struct cannot be mutated. Keep the original constant or declare a mutable instance for an intentional update.

Distinguish value copies from shared members

A struct can contain references to class instances, so copying the struct does not necessarily clone every object reachable from it. Distinguish value semantics of the wrapper from reference semantics of its members.

Check your decision

Can original.addBonus() compile while original is let?
Choose one answer. You can try again.

Answer and explanation

No.

No. The mutating method changes the struct value, and a let-bound struct cannot be mutated. Keep the original constant or declare a mutable instance for an intentional update.

Check the practical consequence

A struct contains a class reference. What does copying the struct imply?
Choose one answer. You can try again.

Answer and explanation

The wrapper is copied logically, but both wrappers may still refer to the same class object.

A struct can contain references to class instances, so copying the struct does not necessarily clone every object reachable from it. Distinguish value semantics of the wrapper from reference semantics of its members.

Distinguish value copies from shared members

No. The mutating method changes the struct value, and a let-bound struct cannot be mutated. Keep the original constant or declare a mutable instance for an intentional update. A struct can contain references to class instances, so copying the struct does not necessarily clone every object reachable from it.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Classes and shared references →