<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);">This inconsistency could be solved by giving optionals a method to specifically perform side effects.&nbsp;</span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);">In the same way that arrays have a 'map' function for retuning new arrays and a 'forEach' to return '()' and perform a side effects. Option could have a 'forEach' method that returns '()'.&nbsp;</span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);">s.map(print) // would trigger a "unused result warning"</span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);">s.forEach(print) // would not trigger any warning</span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);">The naming is definitely debatable, but I think the idea is worth considering.&nbsp;</span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);"><br>Andre Videla&nbsp;</span></div></div><div id="AppleMailSignature"></div><div><br>On 31 Jan 2017, at 17:47, Alex Hoppen via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8">Amendment to the history of the bug after I had a look at the bug reports again:&nbsp;<a href="https://bugs.swift.org/browse/SR-1895" class="">SR-1895</a>&nbsp;explicitly asked that&nbsp;<div class=""><br class=""><div class=""><div class="">let s: String? = "hi"</div><div class="">s.map {print($0)}</div><div class=""><br class=""></div><div class="">should not produce any warnings while it did so during beta 1.&nbsp;</div><div class=""><br class=""></div><div class="">– Alex</div></div></div><div class=""><br class=""></div><div style=""><blockquote type="cite" class=""><div class="">On 31 Jan 2017, at 09:07, Alex Hoppen via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">This was a deliberate change between Swift 3 beta 1 and beta 2 after a friend of mine pointed the following inconsistency out to me:<br class=""><br class="">struct Foo {<br class=""> &nbsp;func bar() {}<br class="">}<br class="">let foo: Foo? = Foo()<br class="">foo?.bar() // Does not create a warning<br class="">true ? foo?.bar() : foo?.bar() &nbsp;// expression of type '()?' is unused<br class=""><br class="">After some offline discussion at WWDC with the Swift team we decided to move to a consistent model where ()?, ()??, … is always discardable since we didn't want to take the convenience of foo?.bar() away (something that regularly occurs with weak variables, e.g. captures in closures).<br class=""><br class="">So much for the history of this feature.<br class=""><br class="">– Alex<br class=""><br class=""><br class=""><blockquote type="cite" class="">On 30 Jan 2017, at 22:58, Daniel Duan via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">Hi all,<br class=""><br class="">Right now, expressions that evaluates to Optional&lt;()&gt;, Optional&lt;Optional&lt;()&gt;&gt;… gets special treatment when it’s unused. For example:<br class=""><br class="">func f(s: String) {}<br class="">let s: String = “”<br class="">s.map(f) // no warning here, even tho the resulting type is `Optional&lt;()&gt;` and unused.<br class=""><br class="">func g() throws {}<br class="">try? g() // no warnings here neither.<br class=""><br class="">This is convenient, but encourages composing map/filter/reduce, etc with side-effect-ful functions, which we have found a few cases of in our production code recently. Granted, these cases could’ve been caught with more careful code reviews. But we wouldn’t have missed them if this “feature” didn’t exist.<br class=""><br class="">I think we should remove the special treatment so that code in the example above would generate a warning about `()?` being unused. Users can silence it manually by assigning the result to `_`. <br class=""><br class="">OTOH, this would undermine the convenience of `try?` when the throwing function don’t return anything.<br class=""><br class="">What do y’all think?<br class=""><br class="">Daniel Duan<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class=""></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>