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.
Result types — Result<T, Error> is a free Swift Academy lesson on CoddyKit — lesson 3 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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().
Frequently asked questions
Is the “Result types — Result<T, Error>” lesson free?
Yes — the full text of “Result types — Result<T, Error>” is free to read here on the web, and the Swift Academy course includes 3 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 “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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
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
- Optional chaining & error handling basics
- do/catch & defining errors (enums)
- Result types — Result