Due to the lack of generic support in typealias definitions in Swift, I wrote a generic ResultHandler<T> callback like so:
struct ResultHandler<T> {
let f: (result: T) -> ()
}
This works great for handlers that use anything other than Void:
Dispatch<Bool>.dispatch({ () -> (Result<Bool>) in
Result<Bool>(true)
}).onResult(ResultHandler() { (result: Bool) in
if result {
self.doSomething() // Method executes successfully
}
})
But when attempting to use Void:
Dispatch<Void>.dispatch({ () -> (Result<Void>) in
Result<Void>()
}).onResult(ResultHandler() { (result: Void) in // Compiler error here
self.doSomething()
})
There is a compilation error:
Function signature '(Void) -> ()' is not compatible with expected type '(result: Void) -> ()'
Which seems misleading/a compiler bug, as if I change the signature to use a 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 up the optional-indicating ?, but still does not properly detect my result: label:
Function signature '(Void?) -> ()' is not compatible with expected type '(result: Void) -> ()'
What am I doing wrong?