Precise error typing in Swift

There is of course a middle ground, that is, anonymous enums, that would be for enums what tuples are right now for structs.

I'll not go down this path again, I tried to show their utility in the past but given that they would represent the dual structure to tuples, they are pretty clearly useful for the dual reasons. Being able to sum multiple error types without the need to create a specific enum only for that is just another example. The most compelling arguments I read against this are mostly related to clarity of syntax, but for injection/projection we could actually use ._0, ._1, which would match the synthesized Codable keys recently added to Swift. Anonymous enums would follow the same protocol conformance rules we're adding for tuples, that is, Equatable, Hashable, Comparable, and we would need to add Error if all cases are Error.

Multiple Either# types added to the standard library would also work, but would be worse in terms of flexibility (no option to declare specific tags for cases), and would essentially represent a load of boilerplate.

2 Likes