Created
May 6, 2020 09:18
-
-
Save idrougge/454893e6fba164c1762395df6950c47a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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