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

Mathieu Godart m at godart.co
Thu Feb 25 02:10:23 CST 2016



> Le 25 févr. 2016 à 08:50, Haravikk via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> 
>> On 25 Feb 2016, at 05:19, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> These lack conciseness. What about the already supported: 
>> 
>> let _ = ingoreResult()
> 
> This could work until we gain some other, general purpose, method to suppress warnings, though it isn’t pretty IMO. For example, if a class’ methods are designed to be chain able then the result isn’t necessary (it’s just self), so in things like loops you could end up having to use let _ an awful lot, like so:
> 
> 	class Chainable {
> 		var value = 0
> 		func add(add:Int) {
> 			value += add
> 			return self
> 		}
> 	}
> 
> 	let chainable = Chainable()
> 	for _ in 0 ..< 10 {
> 		if getSomeCondition() {
> 			let _ = chainable.add(5)
> 		} else {
> 			let _ = chainable.add(2)
> 		}
> 	}
> 
> Stupid example, but imagine if there were a few other operations, perhaps handled by other conditionals, then suddenly you end up with a lot of these let _ statements, which is why being able to suppress the warnings in all cases is still desirable.

You convince me here. The two ways to ensure we do this deliberately seem useful and not conflicting. 

> On the topic of verbosity in the attribute, I don’t think there’s any need to go to four words; the current attribute is @warn_unused_result, and I think that @ignore_unused_result works a direct opposite (i.e- the result being unused is still strange to the compiler, but it chooses not to issue a warning about it). I’m only using snake case because that’s what the current value has, obviously we’ll adjust for whatever the final attribute naming convention turns out to be.

+1 Coherent, expressive and short!

Mathieu



> _______________________________________________
> 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/20160225/c4d83bda/attachment.html>


More information about the swift-evolution mailing list