Swift Academy · Lesson

Inference in Closures

Understand parameter and return inference in closures.

Lesson 4 of 413 steps

Inference in Closures is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Closures Infer Types

Inside many contexts, a closure can infer both its parameter and return types from how it is used.

let numbers = [1, 2, 3, 4]
let doubled = numbers.map { $0 * 2 }
print(doubled)

Inferred Parameter Type

When you pass a closure to map, the element type tells Swift what $0 is, so you can omit the parameter type.

let names = ["ada", "bob"]
let upper = names.map { $0.uppercased() }
print(upper)

Inferred Return Type

The body of the closure determines its return type. Returning a Bool makes the closure a predicate.

let nums = [1, 2, 3, 4, 5]
let evens = nums.filter { $0 % 2 == 0 }
print(evens)

Shorthand Argument Names

$0, $1, and so on refer to closure arguments by position when their types are inferred.

let sums = zip([1, 2, 3], [10, 20, 30]).map { $0 + $1 }
print(Array(sums))

Explicit Params When Needed

You can still name parameters and annotate types if it improves clarity.

let nums = [3, 1, 2]
let sorted = nums.sorted { (a: Int, b: Int) -> Bool in
    return a < b
}
print(sorted)

Inference From Context

If you assign a closure to a typed variable, the variable's type tells the closure what its signature is.

let add: (Int, Int) -> Int = { a, b in a + b }
print(add(2, 3))

Trailing Closure

Trailing closure syntax keeps inference working while making call sites readable.

let result = [5, 3, 8, 1].sorted { $0 > $1 }
print(result)

Inferred in reduce

In reduce, the accumulator and element types are inferred from the initial value and the sequence.

let total = [1, 2, 3, 4].reduce(0) { acc, x in acc + x }
print(total)

Returning From a Closure

Single-expression closures can omit the return keyword; the value of the expression is returned and its type inferred.

let lengths = ["hi", "hello", "hey"].map { $0.count }
print(lengths)

Type-Annotated Closure Variable

When stored without an immediate call, annotate the closure type so Swift can infer the body.

let transform: (String) -> Int = { $0.count }
print(transform("swift"))

When You Must Annotate

If the surrounding context gives no type information, Swift cannot infer closure parameters and you must annotate them.

let isPositive: (Int) -> Bool = { n in n > 0 }
print(isPositive(5), isPositive(-3))

Quick Check

Test closure inference.

Recap

Closures infer parameter and return types from context such as map, filter, or a typed variable. Use shorthand $0/$1 when types are inferred, and annotate explicitly when no context is available.

let f: (Int) -> Int = { $0 + 1 }
let xs = [1, 2, 3].map { $0 * 10 }
print(f(9), xs)
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Inference in Closures” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Inference in Closures”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Inference in Closures”?

Understand parameter and return inference in closures. 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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Inference in Closures” 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. How Type Inference Works
  2. When to Add Explicit Annotations
  3. Inference with Literals
  4. Inference in Closures
← Back to Swift Academy