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

Erica Sadun erica at ericasadun.com
Wed Mar 2 14:20:03 CST 2016


Modified accordingly:

Under this proposal, the Swift compiler emits a warning when any method or function that returns a non-void value is called without using its result. To suppress this warning, the developer must affirmatively mark a function or method, allowing the result to be ignored. It can be argued that such an override is unnecessary as Swift offers a mechanism to discard the result:

```swift
let _ = discardableResult()
```

This proposal argues that while this workaround makes it clear that the consumption of the result is intentionally discarded, it offers no traceable intent as to whether the API designer meant for this use to be valid.  Forcing an attribute ensures the discardable return value use is one that has been considered and approved by the API author.

-- E



> On Mar 2, 2016, at 1:13 PM, David Owens II via swift-evolution <swift-evolution at swift.org> wrote:
> 
> It feels like there are three basic groups of APIs:
> 
> An API that returns a result that should be used.
> An API that returns a result that can be used.
> An API that returns no result (void).
> 
> I completely agree that if a result is returned, the intent of the API is more than likely expecting the caller to use it.
> 
> My primary preference is to just say that's the way it is. I don't particular like any of the attributes. Also, we have a mechanism to discard the result already:
> 
> let _ = discardableResult()
> 
> For resiliency, is adding a `@discardable` attribute a breaking change? Removing it?
> 
> So +1 on changing the default to always warn on unused results, -1 on implementing an attribute to allow API designers to mark it as discardable; I'd rather have a `void` overload that did it instead an attribute.
> 
> -David
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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


More information about the swift-evolution mailing list