Swift4: Void for success callback
jc-paris opened this issue · 9 comments
Hi,
I have a weird error since I upgraded my code to swift4:
error: missing argument for parameter #1 in call
promise.success()
^
<#Void#>
BrightFutures.Promise:16:17: note: 'success' declared here
public func success(_ value: T)
The compiler is claiming an argument, if I insert Void promise.success(Void)
it will complain that this method doesn't have any argument.
Is there any workaround ?
Thanks!
JC
This is super annoying and I'm currently dealing with this too. The work around that I've been using is just to place Void()
as a parameter. (i.e.: promise.success(Void())
) It isn't the prettiest thing in the world but it gets the job done.
Hi. Sorry to hear you're having problems with BrightFutures.
I've encountered this issue too when upgrading to Swift 4. In BF, we currently solve it by calling .success(())
. It doesn't look pretty, but it works.
If it turns out that this is an issue with BF and not Swift in general, I'd be open to having an extension on Future/Promise that has a zero-argument success
when its value is Void
.
Void is defined as an empty tuple in Swift, so that is why it works. I agree that your idea might be cleaner.
Does anyone know if this is an advertised change in Swift 4 or have an idea about a related proposal that may have introduced this behaviour?
This is a tricky one I think because we can’t simply add a success
case without an associated value nor a static no-arg success()
factory function to produce a .success(())
.
I quickly tried adding this:
public extension Promise where T == Void {
public func success() {
future.success(())
}
}
And with that in place, I cal call promise.success()
if the Promise has Void as a value. I think this can work. It is however a workaround for a change in Swift that goes beyond BrightFutures, so I don't feel like we have to add it, but if there's demand, it can be added. Let me know what you think!
I think that as you said, it goes against the new swift version. I wouldn't change anything to the framework for now. We just need to adapt :-) Not a big deal !
I don’t think this is a question of going against the language. The change in Swift 4 has made generic void returns and parameters more unpleasant to work with—if the change is here to stay in the language, I think it is fine to make the API more pleasant to use in this case.