[swift-evolution] Make non-void functions @warn_unused_result by default

Pyry Jahkola pyry.jahkola at iki.fi
Wed Mar 2 12:33:18 CST 2016


+1 on making warn-unused-result the default.

> On 02 Mar 2016, at 19:58, Gwendal Roué via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Some may import some foreign (yet valid) patterns like having the remove() function of a collection return the removed element (I didn’t have to look too far way: here is a lib with much hype theses days which does exactly that: https://github.com/lorentey/BTree/blob/master/Sources/BTree.swift#L507)
> 
> See?

That doesn't seem like a great example because the very same library is making extensive use of @warn_unused_result at places where it matters. So of course it has a remove() method with a discardable, yet unannotated, return type. What else could it do since we don't have any such attribute yet!

I'm sure BTree will be among the first to adopt the new `@ignore_unused_result` or `foo() -> @discardable Result` attribute, whichever we may choose if the proposal gets accepted.

There will surely be libraries with unannotated functions whose return type is intended to be discarded. Some of them will be in Objective-C, which might be worth considering in the proposal. I believe it's a self-fixing problem however. If people feel the libraries are clumsy to use with all the `_ = x.foobar()` business, they'll file bug reports and the maintainers will just add the needed attribute that removes the need for explicitly discarding the result. That's a non-breaking change after all.

PS. You don't need any `let` when discarding the result. The underscore is enough.

— Pyry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160302/222486a9/attachment.html>


More information about the swift-evolution mailing list