Hi
Using Swift 2.1, XCode 7.1, PromiseKit 3.0.0 I frequently have to be pretty explicit about types in "then" blocks:
NSURLConnection.POST(AuthUrl, JSON: params).asDictionary().then { data in }
// Error: Missing return in a closure expected to return 'AnyPromise'
return NSURLConnection.POST(AuthUrl, JSON: params).asDictionary().then { (data) -> () in }
// Error: Cannot convert value of type '_ -> ()' to expected argument type '(NSDictionary) -> AnyPromise'
All-in-all, I tend to have to spend a while fiddling with the types to get things to compile whereas the documentation almost universally lists code that doesn't specify any types at all. Is there some trick to get this to work with less fiddling? Failing that, is there some convenient way for me to create an already-resolved AnyPromise so I can return a value when the compiler demands one?
No sorry. These are swift compiler bugs. I hate it too and hope further iterations of swift will be smarter about inferring closure return types.
One trick is to write it in one line. Another is wrapping it in a func.
I think there is more going on here than type inference failure. Nothing is working for me here. Do you have any other suggestions?
return NSURLConnection.POST(AuthUrl, JSON: params).asDictionary().then { data in
Auth.setContact(data)
}
// Cannot convert value of type '()' to closure result type 'AnyPromise'
return NSURLConnection.POST(AuthUrl, JSON: params).asDictionary().then { data -> () in
Auth.setContact(data)
}
// Declared closure result '()' is incompatible with contextual type 'AnyPromise'
return NSURLConnection.POST(AuthUrl, JSON: params).asDictionary().then { data -> AnyPromise in
Auth.setContact(data)
return AnyPromise(bound: Promise<Void>(Void()))
}
// Cannot invoke 'then' with an argument list of type '((NSDictionary) throws -> AnyPromise)'
return NSURLConnection.POST(AuthUrl, JSON: params).asDictionary().then(body: { (data: NSDictionary) throws -> AnyPromise in
return AnyPromise(bound: Promise<Void>(Void()))
})
// Cannot invoke 'then' with an argument list of type '((NSDictionary) throws -> AnyPromise)'
return NSURLConnection.POST(AuthUrl, JSON: params).asDictionary().then(Auth.setContact)
// Cannot invoke 'then' with an argument list of type '((NSDictionary) throws -> AnyPromise)'
Note that none of the functions above throw anything. The compiler is just coming up with that on its own.
What is the signature for Auth.setContact?
As it's common for Swift to give wrong errors based on errors inside the closure.
It was (NSDictionary) -> AnyPromise.
Given that this was failing too I don't think it had to do with an error in the closure:
return NSURLConnection.POST(AuthUrl, JSON: params).asDictionary().then(body: { (data: NSDictionary) throws -> AnyPromise in
return AnyPromise(bound: Promise<Void>(Void()))
})
You can try
return NSURLConnection.POST(AuthUrl, JSON: params).asDictionary().then { data -> Void in
Auth.setContact(data)
}
seemed to solve the problem for me
Probably here the problem was the return type of the enclosing function had a different promise type to that inferred by the return from the then.
Closing, we can't fix this, it's up to Swift. The best we could do is have three thens, one for returning a value, one for returning a promise and one for returning an AnyPromise. Then Swift would not be confused.
Though it is notable it would not fix any of the above errors, but hopefully the messages would be a little more helpful.
Most helpful comment
You can try
seemed to solve the problem for me