0Pricing
Swift Academy · Lesson

Non-mutating Alternatives

Return new values instead of mutating in place.

Non-mutating Alternatives is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Return Instead of Mutate

Instead of changing an instance in place, a non-mutating method can return a brand-new value with the change applied.

struct Counter {
    var value: Int
    func incremented() -> Counter {
        return Counter(value: value + 1)
    }
}
let c = Counter(value: 0)
let next = c.incremented()
print(c.value, next.value)

Original Stays Unchanged

Because nothing is mutated, the original value is preserved and a new value is produced.

struct Vector {
    var x: Int
    var y: Int
    func scaled(by factor: Int) -> Vector {
        return Vector(x: x * factor, y: y * factor)
    }
}
let v = Vector(x: 2, y: 3)
let big = v.scaled(by: 10)
print(v.x, v.y, big.x, big.y)

Works on let Instances

Non-mutating methods can be called on let constants, since they do not change the receiver.

struct Money {
    var cents: Int
    func plus(_ other: Int) -> Money {
        return Money(cents: cents + other)
    }
}
let m = Money(cents: 100)
print(m.plus(50).cents)

Naming Convention

Swift convention: mutating methods use imperative verbs (sort), non-mutating ones use past-participle or -ed/-ing forms (sorted).

var nums = [3, 1, 2]
let ordered = nums.sorted()
nums.sort()
print(ordered, nums)

sorted vs sort

The standard library follows this convention: sorted() returns a new array, sort() mutates in place.

let a = [5, 2, 8, 1]
let b = a.sorted()
print(a)
print(b)

reversed vs reverse

Similarly reversed() is non-mutating. Here we materialize it with Array.

let a = [1, 2, 3]
let b = Array(a.reversed())
print(a, b)

Chaining Non-mutating Calls

Non-mutating methods return values, so you can chain them fluently.

let result = [4, 2, 9, 1].sorted().map { $0 * 2 }
print(result)

Functional Style

Returning new values instead of mutating supports a functional style that is easy to test and reason about.

struct Account {
    var balance: Int
    func deposited(_ n: Int) -> Account {
        return Account(balance: balance + n)
    }
}
let a0 = Account(balance: 0)
let a1 = a0.deposited(100).deposited(50)
print(a0.balance, a1.balance)

Avoiding Shared State Bugs

Producing new values avoids accidental shared mutations, which is especially valuable across threads.

struct Settings {
    var volume: Int
    func with(volume newVolume: Int) -> Settings {
        return Settings(volume: newVolume)
    }
}
let base = Settings(volume: 5)
let loud = base.with(volume: 11)
print(base.volume, loud.volume)

When to Mutate Instead

If you are repeatedly updating one instance in a loop, in-place mutation can be more efficient than creating many copies.

var total = 0
for n in 1...5 {
    total += n
}
print(total)

Offer Both

Well-designed types sometimes offer both forms, like the standard library, so callers choose mutation or a fresh value.

struct Text {
    var value: String
    func uppercased() -> Text {
        return Text(value: value.uppercased())
    }
    mutating func uppercase() {
        value = value.uppercased()
    }
}
var t = Text(value: "hi")
let u = t.uppercased()
t.uppercase()
print(u.value, t.value)

Quick Check

Test non-mutating design.

Recap

As an alternative to mutation, a non-mutating method returns a new value with the change applied, leaving the original intact and working on let instances. Swift convention pairs imperative names (sort) with -ed/-ing names (sorted).

let a = [3, 1, 2]
let b = a.sorted()
print(a, b)

Frequently asked questions

Is the “Non-mutating Alternatives” lesson free?

Yes — the full text of “Non-mutating Alternatives” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Non-mutating Alternatives”?

Return new values instead of mutating in place. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Non-mutating Alternatives” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Value Types and Immutability
  2. The mutating Keyword
  3. Mutating and self Reassignment
  4. Non-mutating Alternatives
← Back to Swift Academy