<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="">Apologies for re-posting this, but I got some message undelivered e-mails for it so I'm not 100% sure if it went through the first time or not:<div><font color="#5856d6" class=""><br class=""></font><blockquote type="cite" class=""><div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto;" class="">On 31 Jan 2017, at 09:07, Alex Hoppen via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></blockquote><font color="#00afcd" class=""><br class=""></font>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=""><font color="#00afcd" class=""><br class=""></font>struct Foo {<br class=""> 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() // expression of type '()?' is unused<br class=""><font color="#00afcd" class=""><br class=""></font>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=""><font color="#00afcd" class=""><br class=""></font><div class=""><div class="">So much for the history of this feature.</div></div></div></blockquote><br class="">Thanks for clarifying, but this is an interesting case actually; it seems the problem here is the ternary, as presumably the following would have worked just fine:<br class=""><font color="#5856d6" class=""><br class=""></font></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><div class=""><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">if</span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">true</span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> { foo?.bar() }</span></div></div><div><div class=""><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">else</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>{ foo?.bar() }</span></div></div></div></div></blockquote><div><font color="#5856d6" class=""><br class=""></font>So your example's problem seems to stem then from the fact that the ternary's type isn't inheriting the discardable nature of the two branches. I wonder then if an alternative solution might to have discardable be an inheritable property, while keeping optional chaining implicitly discardable?<br class=""><font color="#5856d6" class=""><br class=""></font>For example:<br class=""><font color="#5856d6" class=""><br class=""></font></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><div class=""><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">@discardableResult</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">func</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> foo() -> </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Int</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> { </span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">return</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);">1</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> }</span></div></div><div><div class=""><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">func</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> bar() -> </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Int</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> { </span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">return</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);">2</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> }</span></div></div><div><div class=""><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">struct</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> Baz { </span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">func</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> baz() {}}</span></div></div><div><div class=""><font color="#bb2ca2" face="Menlo" class=""><span style="font-size: 11px;" class=""><br class=""></span></font></div></div><div><div class=""><blockquote class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);"><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"></span></div></div></div></div></blockquote></div></div><div><div class=""><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">let</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> a:</span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Baz</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">? = </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Baz</span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">()</span></div></div><div><div class=""><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">true</span><span class="" style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> ? </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);">foo</span><span class="" style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">() : </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);">bar</span><span class="" style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">() </span><span class="" style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">// type is Int, should produce a warning</span></div></div><div><div class=""><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">true</span><span class="" style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> ? </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);">foo</span><span class="" style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">() : </span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);">foo</span><span class="" style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">() </span><span class="" style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">// type is discardable Int, no warning necessary</span></div></div><div><div class=""><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);"><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">true</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>?<span class="Apple-converted-space"> </span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">a</span><span class="" style="font-variant-ligatures: no-common-ligatures;">?.</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);">baz</span><span class="" style="font-variant-ligatures: no-common-ligatures;">() :<span class="Apple-converted-space"> </span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">a</span><span class="" style="font-variant-ligatures: no-common-ligatures;">?.</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);">baz</span><span class="" style="font-variant-ligatures: no-common-ligatures;">()<span class="Apple-converted-space"> </span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">// type is discardable Void, no warning necessary</span></div></div></div></div></div></div></blockquote><div><div class=""><blockquote class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);"><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal; min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"></span></div></div></div></div></blockquote></div></div><div><div>The idea basically being that @discardableResult becomes a property of return types that is passed for as long as it is in common, but does not prevent two types (one discardable, one not) from being equal.</div><font color="#5856d6" class=""><br class=""></font>This might give a best of both? As method chaining producing implicitly discardable results would allow your example to behave as expected, but other cases can still be configured as desired with @discardableResult (or not).</div></body></html>