Skip to content

Instantly share code, notes, and snippets.

@kateinoigakukun
Last active May 2, 2020 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kateinoigakukun/9d4c882822a97955f617b45fac5caf6c to your computer and use it in GitHub Desktop.
Save kateinoigakukun/9d4c882822a97955f617b45fac5caf6c to your computer and use it in GitHub Desktop.
func id<T>(_ v: T) -> T { v }
func produceError() throws {}
// throws version
func f(_ a: () throws -> Void) rethrows { try a() }
f { }
try f { try produceError() }
try id(f)({ }) // Never `throw` but need `try` 😢
try id(f)({ try produceError() })
// Result version
func g<E>(_ a: () -> Result<Void, E>) -> Result<Void, E> { a() }
g { Result<Void, Never>.success(()) } // Result<Void, Never>
g { Result { try produceError() } } // Result<Void, Error>
id(g)({ Result<Void, Never>.success(()) }) // Result<Void, Never> 😎
id(g)({ Result { try produceError() } }) // Result<Void, Error>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment