xcode - Workaround/resolution for Swift compiler error related to Void callback defined in struct -
due lack of generic support in typealias definitions in swift, wrote generic resulthandler<t> callback so:
struct resulthandler<t> {     let f: (result: t) -> () }   this works great handlers use other void:
dispatch<bool>.dispatch({ () -> (result<bool>) in     result<bool>(true) }).onresult(resulthandler() { (result: bool) in     if result {         self.dosomething() // method executes     } })   but when attempting use void:
dispatch<void>.dispatch({ () -> (result<void>) in     result<void>() }).onresult(resulthandler() { (result: void) in // compiler error here     self.dosomething() })   there compilation error:
function signature '(void) -> ()' not compatible expected type '(result: void) -> ()'
which seems misleading/a compiler bug, if change signature use wrongly-typed value:
dispatch<void>.dispatch({ () -> (result<void>) in     result<void>() }).onresult(resulthandler() { (result: void?) in // compiler error here     self.dosomething() })   the error message picks optional-indicating ?, still not detect result: label:
function signature '(void?) -> ()' not compatible expected type '(result: void) -> ()'
what doing wrong?
you don't need specify parameter names in closure type declarations—that's what's giving compiler problems:
struct resulthandler<t> {     let f: t -> () }   now can create resulthandler instances use bool parameter, void parameter, or leave out parameter altogether:
let r1 = resulthandler() { (result: bool) in     if result {         //     } }  let r2 = resulthandler() { (_: void) in     // }  let r3 = resulthandler() {     // }      
Comments
Post a Comment