<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 19, 2016, at 6:59 AM, Chris Lattner <<a href="mailto:clattner@apple.com" class="">clattner@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=windows-1252" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jun 16, 2016, at 12:44 PM, Charlie Monroe via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Sorry, didn't have time to actually submit the proposal, but it at least gave me some time to think it through a few more times.<div class=""><br class=""></div><div class="">After reconsiderations, I suggest the following:</div><div class=""><br class=""></div><div class="">- deprecation of interpolation of optionals.</div><div class="">- extending the Optional type by:</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>- <b class="">var detailedDescription: String</b> - which will return the same value as until now: "nil" for nil and "Optional(value)" for .some(_). This is due to .description and .debugDescription's documentation discouraging direct invocation.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>- <b class="">func descriptionWithDefaultValue(_ defaultValue: String = "nil") -> String</b> - which would return either the description of the value, or `defaultValue`.</div><div class=""><br class=""></div><div class="">So, when the compiler runs into interpolation of an Optional, it issues a warning with a fix-it, that by default calls .descriptionWithDefaultValue() on the optional.</div></div></div></blockquote><br class=""></div><div class="">I think that this is overcomplicating the issue. I’d suggest that someone just implement a simple compiler patch (which doesn’t need an evolution proposal):</div><div class=""><br class=""></div><div class="">1. Produce a warning when a string interpolation includes a non-redundantly-parenthesized optional, saying that this is probably a bug.</div></div></div></blockquote><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">2. Attach two notes (with fixits) to the warning:</div><div class="">2a. a note that adds redundant parens around the parameter, silencing the warning.</div></div></div></blockquote><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">2b. a note that fixits the issue with a “!” or "?? <#placeholder#>"</div></div></div></blockquote><div><br class=""></div><div>The "?? <#placeholder#>" fix-it suffers from a slight drawback:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">enum</span><span style="font-variant-ligatures: no-common-ligatures" class=""> MyEnum {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> value1</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""> case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> value2</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; min-height: 10px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> someObj: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyEnum</span><span style="font-variant-ligatures: no-common-ligatures" class="">? = </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> obj = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">someObj</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ?? </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Hello" // Error: Binary operator '??' cannot be applied to operands of type 'MyEnum?' and 'String'</span></div><div class=""><br class=""></div></div><div>(tested in Xcode 8)</div><div><br class=""></div><div>I'm not sure whether to consider this a bug in the compiler - what should be the correct behavior? Should "obj" be of kind "Any" with the compiler finding the lowest common type in all cases? Won't this introduce a new kind of confusing bugs where the inferred types would be wrong?</div><div><br class=""></div><div>I've deprecated the interpolation of Optionals in my projects (~200KLOC) using an extension on String and the only way to satisfy the compiler in all cases is to use something like </div><div><br class=""></div><div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">"someObj is </span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">\</span><span style="font-variant-ligatures: no-common-ligatures" class="">((</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">someObj</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Any</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">?) ?? </span><span style="font-variant-ligatures: no-common-ligatures" class="">"Hello")"</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class="">And it can get a bit nastier, which is why I've proposed a more universal and better readable fix:</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">"someObj is </span><span style="font-variant-ligatures: no-common-ligatures;" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">someObj</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.descriptionWithDefaultValue(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">"Hello"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">)"</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class="">and</div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class=""><br class=""></span></div></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">"</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">item.</span><span style="font-variant-ligatures: no-common-ligatures;" class="">valueForPrintableField</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">(.</span><span style="font-variant-ligatures: no-common-ligatures;" class="">DocumentType</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">, withContext: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">).optionaDescription</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">item.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">year</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">) - </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">item.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">itemID</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">).pdf"</span></div><div class=""><br class=""></div></div><div style="margin: 0px; line-height: normal;" class="">I'm personally not a big fan of the extra parentheses around the optional for silencing the warning either, mostly since it will look like this:</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"</span><span style="font-variant-ligatures: no-common-ligatures" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">((</span><span style="font-variant-ligatures: no-common-ligatures" class="">item.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">valueForPrintableField</span><span style="font-variant-ligatures: no-common-ligatures" class="">(.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">DocumentType</span><span style="font-variant-ligatures: no-common-ligatures" class="">, withContext: </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures" class="">))</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">) </span><span style="font-variant-ligatures: no-common-ligatures" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">item.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">year</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">) - </span><span style="font-variant-ligatures: no-common-ligatures" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">item.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">itemID</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">).pdf"</span></div></div></div></div><div><br class=""></div><div>which is IMHO hard to read and understand why there are so many parentheses.</div><div><br class=""></div><div>But if you insist on this being the way to go, I'll try to take a look at the compiler, though it's been a while since I've done anything in Clang.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">I agree that this should be fixed.</div><div class=""><br class=""></div><div class="">-Chris</div><br class=""></div></div></blockquote></div><br class=""></body></html>