Skip to content

Instantly share code, notes, and snippets.

@PaulTaykalo
Last active March 22, 2022 17:44
Show Gist options
  • Save PaulTaykalo/b595d65903b75d85d65e5b5a9c256229 to your computer and use it in GitHub Desktop.
Save PaulTaykalo/b595d65903b75d85d65e5b5a9c256229 to your computer and use it in GitHub Desktop.
Strange generic? autoclosure?
struct Err: Error {}
open class ClosureContainer<Closure, Value> {
var arg: Closure?
}
func unwrap<T>(_ expression: @autoclosure () throws -> T?) throws -> T {
guard let value = try expression() else { throw Err() }
return value
}
let handler = ClosureContainer<(String) -> Void, Void>()
handler.arg = { v in print("CALLBACK : \(v)") }
// NOT WORKING! // crash
let callback = try unwrap(handler.arg)
// WORKING // no crash
// let callback = handler.arg!
print(callback("123"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment