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

Paul Ossenbruggen possen at gmail.com
Thu Feb 25 10:12:32 CST 2016


Without the “let” is not objectionable, using your example:

	let chainable = Chainable()
	for _ in 0 ..< 10 {
		if getSomeCondition() {
		   	_ = chainable.add(5)
		} else {
		        _ = chainable.add(2)
		}
	}

Let is unnecessary since we are just throwing away the result we don’t need the keyword. It is optional.  I don’t think @ignoreUnusedResult or the other options looks very nice either. 



> On Feb 24, 2016, at 11:50 PM, Haravikk <swift-evolution at haravikk.me> wrote:
> 
> 
>> On 25 Feb 2016, at 05:19, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org <mailto: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.
> 
> 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.

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


More information about the swift-evolution mailing list