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

Gwendal Roué gwendal.roue at gmail.com
Wed Mar 2 11:51:39 CST 2016


> Le 2 mars 2016 à 18:45, Chris Lattner <clattner at apple.com> a écrit :
> 
>> On Mar 2, 2016, at 9:36 AM, Gwendal Roué <gwendal.roue at gmail.com> wrote:
>> 
>>> The difference between:
>>> 	_ = foo() 
>>> and:
>>> 	foo()
>>> 
>>> is that the former expresses that the result is *intentionally* being discarded.  The developer obviously thought about it in the first case.  In the second case, they may have though about it, or they may not have.
>> 
>> The former only expresses that the result is *intentionally* being discarded when there is no warning ignoring the returned value.
>> 
>> As soon as there is a warning ignoring the returned value, as would be the default if the discussed proposal were accepted, people will start writing `_ = …` just to *silence* the warning. Not to express intentional discard. It will be impossible to derive any intention from `_ = …`. There will always be the shadow of a doubt…
> 
> You’re right that this is possible, but I doubt it is likely.  If I wrote this and got a warning:
> 
>  sin(x)     // warning, result of call to ‘sin’ is unused
> 
> it is possible that someone would blithely slap an “_ =“ on the line to silence the warning, but it is much more likely they’d say “whoops, I forgot something”.

Let’s imagine some code that is less implicit:

	let _ = cart.remove(item)

What does it mean? Is it that the cart object was badly designed and that the result is an error that we ignore (bad)? Is it that the remove() method returns the removed object, as a confirmation of actual removal, that we ignore here (OK)? What is it? What is it?

Gwendal



More information about the swift-evolution mailing list