Skip to content

Instantly share code, notes, and snippets.

@idrougge
Created May 6, 2020 09:18
Show Gist options
  • Save idrougge/454893e6fba164c1762395df6950c47a to your computer and use it in GitHub Desktop.
Save idrougge/454893e6fba164c1762395df6950c47a to your computer and use it in GitHub Desktop.
enum Either<Left, Right> {
case left(Left)
case right(Right)
}
extension Either: Decodable where Left: Decodable, Right: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
do {
self = .left(try container.decode(Left.self))
} catch {
self = .right(try container.decode(Right.self))
throw error
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment