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

Step C schristopher at bignerdranch.com
Thu Mar 3 09:47:00 CST 2016



On Mar 2, 2016, at 8:42 PM, Stephen Celis <stephen.celis at gmail.com> wrote:

>> On Mar 2, 2016, at 7:54 PM, David Owens II via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> Possibly. I'm genuinely interested in the number of APIs like that. Even if we did want that, I would still prefer a non-attribute solution to the problem.
>> Swift already allows overloading based on return-type. One of the problems is that it's just not good at picking the one that you'd want.
>> func removeLast() { let _ = removeLast() }
>> func removeLast() -> Self.Generator.Element { ... }
>> foo.removeLast() // ok, no warning as there is void version
>> let item = foo.removeLast()
> 
> I like this solution and have wanted Swift to be better about disambiguating this in the past.
> 
> This reminds me of another place where I'd like to mark arguments as "discardable" or improve the way Swift disambiguates.
> 
> I'd also love this related overload to work without error:
> 
>    func handler(body: T -> Void)
>    func handler(body: () -> Void)
>    handler { arg in … } // Error: "Ambiguous use of 'handler'"
>    handler { … }
> 
>    // or
>    func handler(body: @discardable T -> Void)
>    handler { … } // no need to specify "_"

I strongly agree as well. If Swift could better disambiguate, we would not meet the discardable annotation. In fact I have tried to write dual APIs, one returning a result and one returning Void, a few times. 

> 
> Stephen


More information about the swift-evolution mailing list