Skip to content

Instantly share code, notes, and snippets.

@omochi
Created June 21, 2019 03:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omochi/b0fe2aed5465057bde9061da1b200768 to your computer and use it in GitHub Desktop.
Save omochi/b0fe2aed5465057bde9061da1b200768 to your computer and use it in GitHub Desktop.
extension Optional {
public mutating func ensure(_ f: () throws -> Wrapped) rethrows -> Wrapped {
if let x = self {
return x
}
let x = try f()
self = x
return x
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment