Adding @discardableResult to `then`
I'd expect promises to return @discardableResult, to avoid compiler warning.
Right now this:
promise.then {
...
}.then {
...
}
Causes compiler to warn about unused result. To prevent you have to do:
_ = promise.then {
...
}.then {
...
}
@mxcl What about me making a PR for this?
Dupes #549, #551, #561, #574, #584.
This was intentional. See the other tickets for the rationale and discussion.
Ah sorry :-) At least it'd be worth to re-work tests, so they don't spam with compiler warnings so much.
I think we need to some work here at least, eg. I use after(interval: _) a lot, there's no reason to warn for that. It cannot fail.
So maybe we should add an UnfailablePromise class.
Also, as an aside, I added a fatalCatch() chain terminator to my local branch, and use that quite a lot.
Also, during my use of PromiseKit, almost always I find that if I am getting this warning it means I am not handling errors. Only occasionally am I making a sub-chain where I know I am handling the error elsewhere and thus adding a _ = prefix is necessary and thus also a good indicator that I know the error is handled elsewhere.
Other than the above mentioned cases where the promise cannot fail.
Another thing on this, this warning almost makes the UnhandledErrorHandler unnecessary, since the Swift compiler is ensuring we handle errors. We may want to consider deprecating it, unless I'm missing something.
The other option of course is to add the @discardableResult annotation.
I feel this would lead to less good code for our users.
But it would lead to less tedious usage for our users.
Honestly, I feel how we have it now is more Swifty. However, we are not Apple, and our popularity to an extent depends on us not being tedious.
Since this is consistently brought up, we might want to add something about it to the FAQ. We could also mention your fatalCatch() suggestion. (Although I'm not sure people are checking the FAQ before creating issues here.)
I agree that something like UnfailablePromise could simplify a several APIs (after, non-throwing recover, non-throwing UnfailablePromise.then) and would therefore be worthwhile.
It would be nice if we could customize the unused result warning to indicate that you're expected to terminate promises with a catch, but I don't think that's currently possible.
I like the idea of UnfailablePromise. I think we can make PromiseKit smart enough, that it analyzes whole chain and decides whether it may fail or not. It would follow simple rules when chaining:
failable + failable -> failable
unfailable + failable -> failable
failable + unfailable -> failable
unfailable + unfailable -> unfailable
The promise would become unfailable if:
- promise body not
throws - was constructed using an unfailable initializer like
Promise { success: ()->Void in } - promise body returned an unfailable promise
So this chain would be unfailable:
func measureUserProgress(_ user: User, timespan: TimeInterval) ->
firstly { () -> Int // user.getScore() doesn't throw, so this promise can't fail
return user.getScore()
}.then { score in // after is unfailable; then {score} is unfailable too
return after(timespan).then { score }
}.then { oldScore in // since user.getScore() doesn't throw, this would be unfailable too
let newScore = user.getScore()
return newScore - oldScore
}
}
measureUserProgress(user, timespan: 5).then { progress in
print("progress: \(progress)")
}
Can we do all these smarts so it is compile time? I didn't think so, but would love to be wrong.
I don't think either (although haven't tried that hard yet).
If there would be some simple logic (forwarding calls) - is it a deal breaker?
Now I think that's doable. I'll try to create a PoC once I got more time.
That's how we can do it:
class UnfailablePromise<T> {
@discardableResult
func then() -> UnfailablePromise<T> { fatalError("we want generic protocols!") }
}
class Promise<T>: UnfailablePromise<T> {
override func then() -> Promise<T> {
return Promise<T>()
}
}
let promise: UnfailablePromise<Int> = Promise<Int>()
promise.then() // no warning
let promise2: Promise<Int> = Promise()
promise2.then() // warn: unused result
However it makes use of feature that is considered a swift 3 bug.
Regarding swift bug, we can:
a) assume that before swift team fixes that bugs, they will give us much needed generic protocols
b) maybe we could do other way around, so UnfailablePromise inherits from Promise. I'll have a think tomorrow.
I don't really think @discardableResult should be inherited. Therefore I made a comment to open that discussion. Once I get some feedback from swift team, I can make a PR here.
I made a significant progress today (branch). Snippet below works very well:
// unfailable + unfailable #1
Promise<Int>.with(5).then { val in
return Promise(value: val + 10)
}
// unfailable + unfailable #2
Promise<Int>.with(5).then { (val: Int) -> Int in
return val + 10
}
// unfailable + failable #1 -> warning
_ = Promise<Int>.with(5).then { val throws -> Int in
return val + 5
}
// failable + unfailable -> warning
let p = Promise<Int>.with(E.dummy).then { val -> Int in
return val + 5
}
p.catch { _ in }
// failable + failable -> warning
_ = Promise(value: 5).then { val throws -> Int in
return val + 5
}
However there are still many unknown parts, so I don't know if this is 100% doable yet.
@mxcl can you please review existing solution to see if it has a potential to get into production? I just don't want to do unnecessary work.
Great stuff.
I haven't looked yet, but I spent my time in transit today trying out a PromiseKit where Promise is based on a protocol Thennable, this would presumably make it easy for UnfailablePromise to a separate protocol and not need to inherit.
Just saying. I'm tired now and can't recall all the reasons I have done this. Will likely push the work to a branch soon so you guys can take a look. Certainly, the code is simpler in many places.
Anyway, will check out your work @FilipZawada thanks, I'm super hyped this is possible.
The experimental-5.x is now beta worthy IMO, I'll be using it in an app from today.
Awesome! I'll give it a try in my side project
There will be frequent updates, also please, your feedback would be wonderful.
What about a suppress error function?
It's very similar to the _ = solution but I think it's more elegant as it clearly states its purpose.
In PMK5 (experimental) I added a cauterize() function, that prints any error and terminates the chain. So yeah, would be ok with this being in PMK4.
It looks very good. Is it possible to implement it with a tag parameter for the print? something like that:
public func cauterize(tag: String? = nil) {
`catch` { error in
if let tag = tag {
print("PromiseKit: [\(tag)] unhandled error:", error)
} else {
print("PromiseKit: unhandled error:", error)
}
}
}