# Precise error typing in Swift

**URL:** <https://forums.swift.org/t/precise-error-typing-in-swift/52045>\
**Category:** Discussion\
**Tags:** error-handling\
**Created:** [September 14, 2021, 9:24am UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045 "2021-09-14T09:24:56Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![cal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cal/32/35557_2.png) [@cal](https://forums.swift.org/u/cal)\
**Post date:** [September 14, 2021, 7:14pm UTC](https://forums.swift.org/t/precise-error-typing-in-swift/52045/12 "2021-09-14T19:14:05Z")

</div>

> [@John\_McCall](#):
>
> ### Type combinators
> 
> Both binary and precise error typing require the type system to be able to express a certain amount of type-level computation: a formula which resolves to some specific type based on its type arguments. These formula will, in fact, actually need to be spellable in source code.
> 
> At the very least, we require an _error union_ , which takes two or more error types T\_i and produces the composite error type thrown from a context that can throw any of T\_i. This has simple computation rules, which happen to be a commutative monoid:
> 
> - `errorUnion(T, Never) == errorUnion(Never, T) == T`
> - `errorUnion(T, T) == T`
> - `errorUnion(T, U) == Error` (if `T != U` dynamically)
> 
> It falls out that `errorUnion(T, Error) == errorUnion(Error, T) == Error` .

If we need union types for error handling, could we introduce this as general-purpose support that supports any types? (e.g. support for any union types like `Int | String`, including `T | Error`)

The [Commonly Rejected Changes](https://github.com/apple/swift-evolution/blob/main/commonly_proposed.md) list seems to mention union types:

> - [Disjunctions (logical ORs) in type constraints](https://forums.swift.org/t/returned-for-revision-se-0095-replace-protocol-p1-p2-syntax-with-any-p1-p2/2855): These include anonymous union-like types (e.g. `(Int | String)` for a type that can be inhabited by either an integer or a string). "[This type of constraint is] something that the type system cannot and should not support."

Type unions have always seemed like a broadly useful idea to me, and I'm not familiar with the background/context around prior discussions. `T | Error` seems like a natural and precedented spelling for a feature like this (that could be broadly useful outside of error handling).

---

_[View the full topic](https://forums.swift.org/t/precise-error-typing-in-swift/52045)._
