<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I discussed the same issue in a WWDC lab with Doug yesterday and we came to the conclusion that warnings about unused return values of type&nbsp;<font face="Menlo" class="">Optional&lt;Void&gt;</font> were a bug. The basic line of argumentation was that if you have<div class=""><br class=""></div><div class=""><font face="Menlo" class="">class Foo {</font></div><div class=""><font face="Menlo" class="">&nbsp; func bar() {}</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">then</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">foo?.bar()</font> returns <font face="Menlo" class="">Optional&lt;Void&gt;</font> but does not warn about an unused result, which is totally expected behaviour.</div><div class=""><br class=""></div><div class="">It feels inconsistent that this behaviour differs if the return value of <font face="Menlo" class="">Optional&lt;Void&gt;</font> is used in a more complicated expression, e.g.</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">true ? foo?.bar() : foo?.bar()</font></div><div class=""><br class=""></div><div class="">Hence we decided that <font face="Menlo" class="">Optional&lt;Void&gt;</font>&nbsp;simply shouldn't generate any warnings about unused results. Note that this does not prevent the user of such an API to still check the result, if he wants to do so.</div><div class=""><br class=""></div><div class="">Implementation of this change is pending in this PR:&nbsp;<a href="https://github.com/apple/swift/pull/3057" class="">https://github.com/apple/swift/pull/3057</a>.</div><div class=""><br class=""></div><div class="">– Alex</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 17 Jun 2016, at 18:26, James Froggatt 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="">More generally, this is the issue of whether the automatic discardability of Void should be extended to Optional&lt;Void&gt;. This could also apply to the result of optional chaining.<br class=""><br class="">EG:<br class="">observer?.update() //returns ()?<br class=""><br class="">compared to:<br class="">if let unwrapped = observer {<br class=""> &nbsp;&nbsp;&nbsp;unwrapped.update() //returns ()<br class="">}<br class=""><br class="">Since ()? (or Void?) is almost certainly a side-effect of optional chaining of some form, I'd be in favour of this extension to the rule.<br class=""><br class=""><br class=""><br class="">As an interesting side note, the counter-point to this change I can think of is one of symmetry, potentially relevant in some high-level generic code. Failable functions often follow the pattern of returning an Optional, which is nil on failure. Extending this rule to, for example, an in-place load() function, one may consider returning ()? to indicate whether the function was successful. This would be deliberate use of Optional&lt;()&gt;, and so it would make sense not to make this particular result discardable, which would be impossible with this change.<br class=""><br class="">The precedent of languages without optionals and a real Void/Unit type has been established, which is to return a Bool value to indicate success, and if this weren't such a fundamental expectation, it's arguable ()? may actually be clearer in these cases: load() -&gt; Bool could be read as a function which loads a Bool value, whereas load() -&gt; ()? is unambiguous.<br class=""><br class="">While this is unlikely to become an established convention, I love that Swift is able to make such ideas possible. :)<br class="">Just the simple things which do away with implementation-driven corner cases in favour of a reliable, clear set of rules, really make the language more powerful.<br class=""><br class="">------------ Begin Message ------------ <br class="">Group: gmane.comp.lang.swift.evolution <br class="">MsgID: &lt;<a href="mailto:B04DCF76-0AA6-4E80-9930-04E1B710E9A0@icloud.com" class="">B04DCF76-0AA6-4E80-9930-04E1B710E9A0@icloud.com</a>&gt; <br class=""><br class="">I propose that the compiler warnings for unused results are removed from try? if the statement being ‘tried’ does not return itself. This is inline with how try works and although try? does have a possible return value itself (nil) the warning isn’t adding/helping any if the result is either going to be nil or void.<br class=""><br class="">When try? is used in this way, it is essentially the caller saying they don’t care if the operation fails and any consequences of that failure will be handled later on.<br class=""><br class="">I have a slightly contrived example here on gist of where this could be useful <a href="https://gist.github.com/joncottonskyuk/abc6caad8be137193d4e1e58cc8d2e06" class="">https://gist.github.com/joncottonskyuk/abc6caad8be137193d4e1e58cc8d2e06</a><br class=""><br class="">basically, in the person model, I don’t always care if the emailAddress is set, but in some cases I do, to differentiate between the two use cases, the caller can choose to use either try when they do care and want to handle the specific error, or try? if they don’t care about the failure and just want to carry on with the usual execution path.<br class=""><br class="">The alternative is to just leave this as it is and the caller must then use _ = try? … to suppress the warnings. However, whilst _ = is very useful for suppressing this warning in most cases, as it shows intent for future maintainers of the code, in this case I don’t think it really adds any value. If the statement being attempted does not return itself then you are left with no choice but to assign to nothing to suppress the warning as opposed to assigning to some local reference and then just throwing that away.<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="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""><br class=""><br class="">------------- End Message ------------- <br class=""><br class=""><br class=""><br class="">From James F<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class="">swift-evolution@swift.org<br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div></body></html>