<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div><br></div><div>Am 01.11.2016 um 19:42 schrieb Erica Sadun via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">Here are a few varieties of how that call might look versus the proposed update for normal pattern matching:</p><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> unwrap .<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">string</span>(myString) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">~=</span> json { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> unwrap .<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">contact</span>(code, <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">_</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">~=</span> response { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> unwrap .<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">contact</span>(code, <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> message) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">~=</span> response { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// vs proposed</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);"></span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> .<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">string</span>(myString) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">~=</span> json { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> .<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">string</span>(myString) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">~=</span> json { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> .<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">contact</span>(<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> code, <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">_</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">~=</span> response { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> .<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">contact</span>(<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> code, <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> message) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">~=</span> response { <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span> }</pre></div><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class="">Although slightly wordier than <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">let</code> and <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">var</code>, the <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">unwrap</code> solution offers advantages:</p><ul style="box-sizing: border-box; padding-left: 2em; margin-top: 0px; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class=""><li style="box-sizing: border-box;" class="">It enhances readability. If the goal is to unwrap an embedded value, <code style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">unwrap</code> uses a more appropriate term.</li><li style="box-sizing: border-box; margin-top: 0.25em;" class="">It establishes <em style="box-sizing: border-box;" class="">one</em> place for the keyword to live instead of the "does it go inside or outside" status quo. A consistent place means prettier code.</li></ul></blockquote><div>The `unwrap` variant loses the ability to distinguish between binding a variable (which means the variable will be preceded by `let`) and matching against the value of a variable, e.g. the following are very different:</div><div><br></div><div>let code = 1</div><div>if case .contact(code, let msg) = response {...}</div><div><br></div><div>vs.</div><div><br></div><div>if case .contact(let code, let msg) = response {...}</div><div><br></div><div>Actually for that proposed use of `unwrap` I see no advantage over the existing `case` which I prefer over the ~= operator. The assignment operator does not look wrong in my eyes, either, because it is quite natural to have bindings happen on the left side of it.</div><div><br></div><div>Using `unwrap` solely as replacement for `let x = x`, i.e. as `unwrap x` is fine with me.</div><div><br></div><div>-Thorsten </div><div><br></div><div><br></div></body></html>