Avatar
↑があれば、たとえば↓のようなことができる。 @rethrows protocol FailureProtocol: Error { func throwSelf() throws -> Never } extension FailureProtocol { func throwSelf() throws -> Never { throw self } } extension Never: FailureProtocol { func throwSelf() -> Never { fatalError() } } struct FooError: FailureProtocol {} @rethrows protocol ResultProtocol { associatedtype Value associatedtype Failure: FailureProtocol func get() throws -> Value } enum Result<Value, Failure: FailureProtocol>: ResultProtocol { case success(Value) case failure(Failure) func get() rethrows -> Value { switch self { case .success(let value): return value case .failure(let error): try error.throwSelf() } } } func useResult<R: ResultProtocol>(_ result: R) rethrows { try print(result.get()) } useResult(Result<Int, Never>.success(42)) try useResult(Result<Int, FooError>.success(42))