<div dir="ltr">Bikeshedding here—is there any way we can make it look like this for enums?<div><br></div><div><font face="monospace, monospace">if let str = unwrap json.string {</font></div><div><font face="monospace, monospace"> // str is the value from the .string enum case</font></div><div><font face="monospace, monospace">}</font></div><div><br></div><div>Essentially, the “unwrap” keyword would treat the last “path component” of what follows as an enum case, and extract the associated value if it matches.</div><div><br></div><div>This could even work with tuple destructuring, as in:</div><div><br></div><div><div><font face="monospace, monospace">guard let (theCode, someMessage) = unwrap myResponse.contact else {</font></div><div><font face="monospace, monospace"> return</font></div><div><font face="monospace, monospace">}</font></div></div><div><br></div><div>And of course we could add a sugared form for optionals to allow the simple, “<font face="monospace, monospace">if unwrap x { … }</font>”.</div><div><br></div><div>I’m not sure if this is where we want to take the idea, it’s just something I thought of.</div><div><br></div><div>If we did go that route, then we might need to disallow enums from having properties with the same names as their cases (otherwise, eg. json.string would refer to a property not a case).</div><div><br></div><div>On the other hand, a slightly different syntax would avoid that issue entirely—for instance,</div><div><br></div><div><font face="monospace, monospace">if let str = unwrap json(.string) { … }</font></div><div>or</div><div><font face="monospace, monospace">if let str = unwrap</font><span style="font-family:monospace,monospace">(.string)</span><font face="monospace, monospace"> json { … }</font></div><div><br></div><div>That last one could even be sugared further when shadowing is desired:</div><div><br></div><div><div><font face="monospace, monospace">if unwrap</font><span style="font-family:monospace,monospace">(.string)</span><font face="monospace, monospace"> json {</font></div><div><font face="monospace, monospace"> // json is a String in here</font></div><div><font face="monospace, monospace">}</font></div></div><div><br></div><div>Potentially worthwhile?<br></div><div><br></div><div>Nevin</div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 1, 2016 at 2:42 PM, Erica Sadun via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word">With massive revisions. I've added Xiaodi Wu to author list. I've combined all unwrapping together. I've added a much more sensible approach (plus a bluesky one) for real world enumerations.<div><br></div><div>Gist with live updates here: <a href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c" target="_blank">https://gist.github.com/<wbr>erica/<wbr>db9ce92b3d23cb20799460f603c0ae<wbr>7c</a></div><div><br></div><div>Send feedback, I will continue to revise.</div><div><br></div><div>-- E</div><div><br></div><div><br></div><div><h1 style="box-sizing:border-box;margin:0px 0px 16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);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";background-color:rgb(255,255,255)">Better Unwrapping</h1><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)"><li style="box-sizing:border-box">Proposal: TBD</li><li style="box-sizing:border-box;margin-top:0.25em">Author: <a href="https://github.com/erica" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">Erica Sadun</a>, <a href="https://github.com/lattner" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">Chris Lattner</a>, <a href="https://github.com/xwu" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">Xiaodi Wu</a> David Goodine</li><span class="gmail-"><li style="box-sizing:border-box;margin-top:0.25em">Status: TBD</li><li style="box-sizing:border-box;margin-top:0.25em">Review manager: TBD</li></span></ul><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);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";background-color:rgb(255,255,255)"><a id="gmail-m_7548809065351022967user-content-introduction" class="gmail-m_7548809065351022967anchor" href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c#introduction" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Introduction</h2><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)">This proposal redesigns common unwrapping tasks:</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)"><li style="box-sizing:border-box">It introduces the <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">unwrap</code> keyword for optional values</li><li style="box-sizing:border-box;margin-top:0.25em">It re-architects <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">guard case</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if case</code> grammar to support unwrapping more complex enumerations by dropping the <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case</code> keyword and replacing <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">=</code> with <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">~=</code>.</li><li style="box-sizing:border-box;margin-top:0.25em">It applies <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">unwrap</code> to non-optional values</li></ul><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)">Swift Evolution threads:</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)"><span class="gmail-"><li style="box-sizing:border-box"><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20161024/028440.html" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">guard let x = x</a></li></span><li style="box-sizing:border-box;margin-top:0.25em"><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/thread.html" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">Using a <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">bind</code> keyword</a>.</li><li style="box-sizing:border-box;margin-top:0.25em"><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20161024/tbd.html" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">Fixing pattern matching grammar</a></li></ul><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);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";background-color:rgb(255,255,255)"><a id="gmail-m_7548809065351022967user-content-motivation" class="gmail-m_7548809065351022967anchor" href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c#motivation" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Motivation</h2><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)">Unwrapping values is one of the most common Swift tasks and it is unnecessarily complex. This proposal simplifies this process and enhances code safety and readability.</p><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;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";background-color:rgb(255,255,255)"><a id="gmail-m_7548809065351022967user-content-optionals" class="gmail-m_7548809065351022967anchor" href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c#optionals" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Optionals</h3><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)">Swift lacks a safe way to bind an optional to a shadowed same-name variable in condition clauses like those used in <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">guard</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if</code> statements.</p><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)">Compare:</p><div class="gmail-m_7548809065351022967highlight gmail-m_7548809065351022967highlight-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.6px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;word-break:normal"><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">guard</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> foo <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> foo <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// redundant</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">guard</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">some</span>(foo) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> foo <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// overly complex</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">guard</span> unwrap foo <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// simple</span></pre></div><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)"><li style="box-sizing:border-box">Using "foo = foo" fails <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">DRY principles</a>. </li><li style="box-sizing:border-box;margin-top:0.25em">Using <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case let .some(foo) = foo</code> or <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case .some(let foo) = foo</code> fails <a href="https://en.wikipedia.org/wiki/KISS_principle" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">KISS principles</a></li></ul><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)"><code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">unwrap</code> guarantees that an unwrapped shadow uses the same name as the wrapped version. This ensures that a conditionally-bound item cannot accidentally shadow another symbol. It eliminates repetition and retains clarity. Further, the <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">unwrap</code> keyword is common, simple to understand, and easy to search for if Swift users are unfamiliar with it.</p><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)">In the rare case that the binding is variable, use the re-imagined <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">var ~=</code> syntax</p><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;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";background-color:rgb(255,255,255)"><a id="gmail-m_7548809065351022967user-content-general-enumerations" class="gmail-m_7548809065351022967anchor" href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c#general-enumerations" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>General Enumerations</h3><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)">Swift's <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">guard case</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if case</code> statements stand out for their unintuitive approach. They look like assignment statements but they are <em style="box-sizing:border-box">not</em> assignment statements. This violates the <a href="https://en.wikipedia.org/wiki/Principle_of_least_astonishment" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">principle of least astonishment</a>. This presents difficulties for new language adopters by combining several concepts in a confusing form. They are arguably underutilized by language experts.</p><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)">The status quo for the <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">=</code> operator is iteratively built up in this fashion:</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)"><li style="box-sizing:border-box"><code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">=</code> performs assignment</li><li style="box-sizing:border-box;margin-top:0.25em"><code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">let x =</code> performs binding</li><li style="box-sizing:border-box;margin-top:0.25em"><code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if let x =</code> performs conditional binding on optionals</li><li style="box-sizing:border-box;margin-top:0.25em"><code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if case .foo(let x) =</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if case let .foo(x) =</code> performs conditional binding on enumerations <em style="box-sizing:border-box">and</em> applies pattern matching</li></ul><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)">Both <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">guard case</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if case</code> statements perform simultaneous pattern matching and conditional binding. Here are examples demonstrating their use in current Swift:</p><pre style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:16px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;color:rgb(51,51,51)"><code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;padding:0px;margin:0px;background-color:transparent;border-radius:3px;word-break:normal;border:0px;display:inline;overflow:visible;line-height:inherit;word-wrap:normal">enum Result<T> { case success(T), error(Error) }
// valid Swift
guard case let .success(value) = result
else { ... }
// valid Swift
guard case .success(let value) = result
else { ... }
</code></pre><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)">The problems with <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">guard case</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if case</code> include:</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)"><li style="box-sizing:border-box">The <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">=</code> operator looks like assignment and not like pattern matching (<code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">~=</code>). </li><li style="box-sizing:border-box;margin-top:0.25em">The <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case</code> layout is both too close to a <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">switch</code>'s <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case</code> but doesn't follow its syntax. In <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">switch</code>, a <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case</code> is followed by a colon, not an equal sign.</li><li style="box-sizing:border-box;margin-top:0.25em">Using the <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case</code> syntax is unneccessarily wordy. It incorporates <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case</code>, <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">=</code>, and optionally <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">let</code>/<code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">var</code> <wbr>assignments.</li></ul><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;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";background-color:rgb(255,255,255)"><a id="gmail-m_7548809065351022967user-content-indirect-and-direct-pattern-matching" class="gmail-m_7548809065351022967anchor" href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c#indirect-and-direct-pattern-matching" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Indirect and Direct Pattern Matching</h3><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)">Swift uses two kinds of pattern matching.</p><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)"><span style="box-sizing:border-box;font-weight:600">Indirect pattern matching</span> such as the kind you see in <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">switch</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">for</code> statements receives an argument in from the statement structure. The argument is not mentioned directly in the <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case</code>:</p><pre style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:16px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;color:rgb(51,51,51)"><code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;padding:0px;margin:0px;background-color:transparent;border-radius:3px;word-break:normal;border:0px;display:inline;overflow:visible;line-height:inherit;word-wrap:normal">switch value {
case .foo(let x): ... use x ...
...
}
for case .foo(let x) in collection { ... }
</code></pre><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)"><span style="box-sizing:border-box;font-weight:600">Direct pattern matching</span> including <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">guard</code>/<code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if</code> <wbr>statements and with the pattern matching operator place the argument to be matched to the right of an operator, either <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">=</code> or <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">~=</code>. The argument is explicitly mentioned:</p><pre style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:16px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;color:rgb(51,51,51)"><code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;padding:0px;margin:0px;background-color:transparent;border-radius:3px;word-break:normal;border:0px;display:inline;overflow:visible;line-height:inherit;word-wrap:normal">if case .foo(let x) = value { ... use x ... }
if 100...200 ~= value { ... }
</code></pre><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)">When using <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if case</code>/<code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">guard case</code> in the absence of conditional binding, statements duplicate basic pattern matching with less obvious semantics. These following two statements are functionally identical. The second uses an assignment operator and the <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case</code> keyword.</p><pre style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:16px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;color:rgb(51,51,51)"><code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;padding:0px;margin:0px;background-color:transparent;border-radius:3px;word-break:normal;border:0px;display:inline;overflow:visible;line-height:inherit;word-wrap:normal">if range ~= myValue { ... } // simpler
if case range = myValue { ... } // confusing
</code></pre><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);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";background-color:rgb(255,255,255)"><a id="gmail-m_7548809065351022967user-content-detailed-design" class="gmail-m_7548809065351022967anchor" href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c#detailed-design" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Detailed Design</h2><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)">This proposal introduces the <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">unwrap</code> keyword. The <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">unwrap</code> statement shadows an enumeration variable to an unwrapped version of the same type. Upon adopting this proposal the following statements produce equivalent behvior:</p><div class="gmail-m_7548809065351022967highlight gmail-m_7548809065351022967highlight-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.6px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;word-break:normal"><span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// New unwrap keyword</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> unwrap myValue { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Existing same-name shadowing</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> myValue <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> myValue { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Existing same-name pattern matching and conditional binding</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">some</span>(<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> myValue) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> myValue { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// old grammar</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">some</span>(myValue) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> myValue { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// old grammar</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Proposed same-name pattern matching and conditional binding</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">some</span>(<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> myValue) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> myValue { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// new grammar</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">some</span>(myValue) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> myValue { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// new grammar</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)">In <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if case</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">guard case</code>, this proposal drops the <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case</code> keyword and replaces the equal sign with the pattern matching operator. The results look like this:</p><div class="gmail-m_7548809065351022967highlight gmail-m_7548809065351022967highlight-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.6px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;word-break:normal"><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">guard</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">success</span>(value) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> result <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">guard</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">success</span>(<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> value) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> result <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">success</span>(value) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> result { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">success</span>(<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> value) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> result { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">guard</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> x<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">?</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> anOptional <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> x<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">?</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> anOptional { <span class="gmail-m_7548809065351022967pl-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)">Users may choose to use <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">var</code> instead of <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">let</code> to bind to a variable instead of a constant. </p><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)">In this update:</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)"><li style="box-sizing:border-box">The <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case</code> keyword is subsumed into the (existing) pattern matching operator</li><li style="box-sizing:border-box;margin-top:0.25em">The statements adopt the existing <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">if-let</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">guard-let</code> <wbr>syntax, including <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">Optional</code> syntactic sugar.</li></ul><div class="gmail-m_7548809065351022967highlight gmail-m_7548809065351022967highlight-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.6px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;word-break:normal"><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> x <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> anOptional { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// current</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> x<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">?</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> anOptional { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// current, would be removed</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> x<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">?</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> anOptional { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// proposed replacement for `if case`</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)">On adopting this syntax, the two identical range tests naturally unify to this single version:</p><div class="gmail-m_7548809065351022967highlight gmail-m_7548809065351022967highlight-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.6px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;word-break:normal"><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> range <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> myValue { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// before</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> range <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> myValue { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// before</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> range <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> myValue { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> } <span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// after</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)">Using pattern matching without conditional binding naturally simplifies to a standalone Boolean condition clause.</p><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;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";background-color:rgb(255,255,255)"><a id="gmail-m_7548809065351022967user-content-unwrap-and-non-optionals" class="gmail-m_7548809065351022967anchor" href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c#unwrap-and-non-optionals" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a><code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">unwrap</code> and Non-Optionals</h3><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)">Real world Swift enumerations rarely follow the Optional pattern, which can be summed up like this:</p><div class="gmail-m_7548809065351022967highlight gmail-m_7548809065351022967highlight-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.6px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;word-break:normal"><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">enum</span> <span class="gmail-m_7548809065351022967pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">OptionalLike</span><<span class="gmail-m_7548809065351022967pl-v" style="box-sizing:border-box;color:rgb(237,106,67)">T</span>> { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">aCaseWithOneAssociatedValue</span>(T)<wbr>, <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">anotherCase</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)">They more typically look like this:</p><div class="gmail-m_7548809065351022967highlight gmail-m_7548809065351022967highlight-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.6px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;word-break:normal"><span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// One generic, one known type</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">enum</span> <span class="gmail-m_7548809065351022967pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">Result</span><<span class="gmail-m_7548809065351022967pl-v" style="box-sizing:border-box;color:rgb(237,106,67)">Value</span>> { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">success</span>(<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Value</span>), <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">failure</span>(<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Error</span>) }
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Many cases of mixed types</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">enum</span> <span class="gmail-m_7548809065351022967pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">JSON</span> {
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">string</span>(<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">String</span>)
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">number</span>(<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Double</span>)
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">boolean</span>(<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Bool</span>)
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">array</span>([JSON])
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">dictionary</span>([<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">String</span>: JSON])
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">null</span>
}
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Multiple associated values</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">enum</span> <span class="gmail-m_7548809065351022967pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">Response</span> { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span> <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">contact</span>(<span class="gmail-m_7548809065351022967pl-en" style="box-sizing:border-box;color:rgb(121,93,163)"><span class="gmail-m_7548809065351022967pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">code</span></span>: <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Int</span>, <span class="gmail-m_7548809065351022967pl-en" style="box-sizing:border-box;color:rgb(121,93,163)"><span class="gmail-m_7548809065351022967pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">message</span></span>: <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">String</span>), <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">failure</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)">You can adapt the <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">bind</code> keyword to work with these real-world cases in one of two ways. The first way uses <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">unwrap</code>instead of <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">let</code> or <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">var</code>. Here are a few varieties of how that call might look versus the proposed update for normal pattern matching:</p><div class="gmail-m_7548809065351022967highlight gmail-m_7548809065351022967highlight-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.6px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;word-break:normal"><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> unwrap .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">string</span>(myString) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> json { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> unwrap .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">contact</span>(code, <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">_</span>) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> response { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> unwrap .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">contact</span>(code, <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> message) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> response { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// vs proposed</span>
<span class="gmail-m_7548809065351022967pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">string</span>(myString) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> json { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">string</span>(myString) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> json { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">contact</span>(<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> code, <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">_</span>) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> response { <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> }
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> .<span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">contact</span>(<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> code, <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> message) <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> response { <span class="gmail-m_7548809065351022967pl-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)">Although slightly wordier than <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">let</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">var</code>, the <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">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)"><li style="box-sizing:border-box">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.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">unwrap</code> uses a more appropriate term.</li><li style="box-sizing:border-box;margin-top:0.25em">It establishes <em style="box-sizing:border-box">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><li style="box-sizing:border-box;margin-top:0.25em">As shown in these examples, it can easily be adapted for variable binding. If you want to override the let behavior, you can insert a var inside the parentheses.</li></ul><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)">A second, riskier, cleaner approach looks like this. It assumes the compiler can pick up and shadow using either the associated value labels (preferred) or the enumeration name (as you'd see with <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">String</code> raw value enumerations):</p><div class="gmail-m_7548809065351022967highlight gmail-m_7548809065351022967highlight-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.6px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-radius:3px;word-break:normal"><span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> unwrap .<span class="gmail-m_7548809065351022967pl-smi" style="box-sizing:border-box">contact</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> response {
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> some compiler magic picks up on the `code` and `message` labels
used <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">for</span> initialization, so the bound variables are `code`
and `message`
}
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> unwrap .<span class="gmail-m_7548809065351022967pl-smi" style="box-sizing:border-box">string</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">~=</span> json {
<span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> use `string` <span class="gmail-m_7548809065351022967pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">here</span> (same name <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">as</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">enum</span> <span class="gmail-m_7548809065351022967pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">case</span>) because
no label was used to define the associated value
}</pre></div><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);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";background-color:rgb(255,255,255)"><a id="gmail-m_7548809065351022967user-content-impact-on-existing-code" class="gmail-m_7548809065351022967anchor" href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c#impact-on-existing-code" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Impact on Existing Code</h2><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)">This proposal is breaking and would require migration.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);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";background-color:rgb(255,255,255)"><a id="gmail-m_7548809065351022967user-content-alternatives-considered" class="gmail-m_7548809065351022967anchor" href="https://gist.github.com/erica/db9ce92b3d23cb20799460f603c0ae7c#alternatives-considered" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Alternatives Considered</h2><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;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);margin-bottom:0px"><li style="box-sizing:border-box">Leaving the grammar as-is, albeit confusing</li><li style="box-sizing:border-box;margin-top:0.25em">Retaining <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">case</code> and replacing the equal sign with <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">~=</code> (pattern matching) or <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-radius:3px">:</code> (to match the switch statement).</li></ul><div><br></div></div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div></div>