[swift-evolution] Make non-void functions @warn_unused_result by default
Dmitri Gribenko
gribozavr at gmail.com
Wed Mar 2 15:25:57 CST 2016
On Wed, Mar 2, 2016 at 1:21 PM, David Owens II <david at owensd.io> wrote:
> Swift's error handling model is optional -> throws -> runtime error. Returning a result is perfectly acceptable as a return value.
But then the result is a critical part of the function operation, and
obviously can't be discarded, for the same reasons why we require
handling the errors.
> And what information are you returning from your side-effect methods that you shouldn't be using? That's what I'm having a hard time trying to grok - the actual use case.
Collection.remove(at:) can return the element it returned for
convenience. It could avoid doing that, and then you would need to
write another line to retrieve the element first if you need it. And
you might need to add another line to store the index, if the index
computation was non-trivial.
let element = myCollection.remove(at: myCollection.index { $i % 0 == 1 })
vs.
let index = myCollection.index { $i % 0 == 1 }
let element = myCollection[index]
myCollection.remove(at: index)
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
More information about the swift-evolution
mailing list