Swift Academy · Lesson

Result types — Result<T, Error>

Represent success or failure without throwing using Result . Create results, switch over them, map/flatMap, and bridge to/from throws.

Lesson 3 of 38 steps

Result types — Result<T, Error> is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 3. 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 3 lessons in total.

Why Result?

Goal: Use Result<T, Error> to return success or failure explicitly—great for callbacks or APIs that avoid throws.

Creating Result

Create .success(value) or .failure(error) instead of throwing.

enum ParseError: Error { case badInt }
func parseInt(_ s: String) -> Result<Int, Error> {
    if let n = Int(s) { return .success(n) }
    else { return .failure(ParseError.badInt) }
}
print(parseInt("42"))
print(parseInt("nope"))

Switch on Result

Handle outcomes with a switch to keep success and failure paths clear.

let r = parseInt("10")
switch r {
case .success(let n): print("number =", n)
case .failure(let e): print("error:", e)
}

Transforming Results

Transform success with map, chain Result-producing work with flatMap, and adapt errors using mapError.

let r1 = parseInt("5").map { $0 * 2 }              // Result<Int, Error>
let r2 = parseInt("x").map { $0 * 2 }                    // still .failure
let r3 = parseInt("8").flatMap { n in                    // chain Results
    n % 2 == 0 ? .success(n/2) : .failure(ParseError.badInt)
}
let rErr = parseInt("x").mapError { $0 }                 // transform error type if needed
print(r1, r2, r3, rErr)

Throws → Result

Use Result { try ... } to capture a throwing call as Result automatically.

enum LoadError: Error { case notFound }
func load(text name: String) throws -> String {
    guard name == "ok" else { throw LoadError.notFound }
    return "loaded"
}
// Build a Result from a throwing call:
let res = Result { try load(text: "ok") }     // .success("loaded")
let res2 = Result { try load(text: "missing") } // .failure(LoadError.notFound)
print(res, res2)

Result → throws

Call get() to rethrow the error from a Result, bridging back to throwing code.

let outcome = parseInt("12")
do {
    let value = try outcome.get()   // throws if .failure
    print("value =", value)
} catch {
    print("thrown from get():", error)
}

Result meaning

Quick check: What does Result<T, Error> represent?

Recap

Recap: Build Result values, handle with switch, transform via map/flatMap/mapError, and bridge to/from throws using the Result { try ... } initializer and get().

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 “Result types — Result<T, Error>” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Result types — Result<T, Error>”, 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 3 lessons in total.

What will I learn in “Result types — Result<T, Error>”?

Represent success or failure without throwing using Result . Create results, switch over them, map/flatMap, and bridge to/from throws. 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 3 of 3.

How long does the “Result types — Result<T, Error>” 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. Optional chaining & error handling basics
  2. do/catch & defining errors (enums)
  3. Result types — Result
← Back to Swift Academy