Last active
March 22, 2022 17:44
-
-
Save PaulTaykalo/b595d65903b75d85d65e5b5a9c256229 to your computer and use it in GitHub Desktop.
Strange generic? autoclosure?
This file contains 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
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