<div dir="ltr">+1 and I would not object to requiring the leading dot for enum declarations.</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 12, 2016 at 9:23 AM, Daniel Steinberg 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">+1 for consistency reasons<div><br><div><blockquote type="cite"><div><div class="h5"><div>On Feb 11, 2016, at 10:00 PM, Erica Sadun via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br></div></div><div><div><div class="h5"><div style="word-wrap:break-word"><a href="https://gist.github.com/erica/e0b8a3a22ab716a19db4" target="_blank">https://gist.github.com/erica/e0b8a3a22ab716a19db4</a><div><br></div><div><h1 style="font-size:2.25em;margin-right:0px;margin-bottom:16px;margin-left:0px;line-height:1.2;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255);margin-top:0px!important">Requiring Leading Dot Prefixes for Enum Instance Member Implementations</h1><ul style="padding:0px 0px 0px 2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)"><li>Proposal: TBD</li><li>Author(s): <a href="http://github.com/erica" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">Erica Sadun</a>, <a href="https://github.com/lattner" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">Chris Lattner</a></li><li>Status: TBD</li><li>Review manager: TBD</li></ul><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)"><a href="https://gist.github.com/erica/e0b8a3a22ab716a19db4#introduction" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Introduction</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)">Enumeration cases are essentially static not instance type members. Unlike static members in structures and classes, enumeration cases can be mentioned in initializers and instance methods without referencing a fully qualified type. This makes little sense. In no other case can an instance implementation directly access a static member. This proposal introduces a rule that requires leading dots or fully qualified references (EnumType.caseMember) to provide a more consistent developer experience to clearly disambiguate static cases from instance members. </p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)"><a href="https://gist.github.com/erica/e0b8a3a22ab716a19db4#motivation" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Motivation</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)">Swift infers the enclosing type for a case on a developer's behalf when the use is unambiguously of a single enumeration type. Inference enables you to craft switch statements like this:</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;padding:16px;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-wrap:normal;word-break:normal"><span style="color:rgb(167,29,93)">switch</span> Coin() {
<span style="color:rgb(167,29,93)">case</span> <span style="color:rgb(167,29,93)">.</span>Heads: <span style="color:rgb(0,134,179)">print</span>(<span style="color:rgb(24,54,145)"><span>"</span>Heads<span>"</span></span>)
<span style="color:rgb(167,29,93)">case</span> <span style="color:rgb(167,29,93)">.</span>Tails: <span style="color:rgb(0,134,179)">print</span>(<span style="color:rgb(24,54,145)"><span>"</span>Tails<span>"</span></span>)
}</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)">A leading dot has become a conventional shorthand for "enumeration case" across the language. When used internally in <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;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">enum</code> implementations, a leading dot is not required, nor is a type name to access the static case member. The following code is legal in Swift.</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;padding:16px;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-wrap:normal;word-break:normal"><span style="color:rgb(167,29,93)">enum</span> Coin {
<span style="color:rgb(167,29,93)">case</span> Heads, Tails
<span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">printMe</span>() {
<span style="color:rgb(167,29,93)">switch</span> <span style="color:rgb(167,29,93)">self</span> {
<span style="color:rgb(167,29,93)">case</span> Heads: <span style="color:rgb(0,134,179)">print</span>(<span style="color:rgb(24,54,145)"><span>"</span>Heads<span>"</span></span>) <span style="color:rgb(150,152,150)">// no leading dot</span>
<span style="color:rgb(167,29,93)">case</span> <span style="color:rgb(167,29,93)">.</span>Tails: <span style="color:rgb(0,134,179)">print</span>(<span style="color:rgb(24,54,145)"><span>"</span>Tails<span>"</span></span>) <span style="color:rgb(150,152,150)">// leading dot</span>
}
<span style="color:rgb(167,29,93)">if</span> <span style="color:rgb(167,29,93)">self</span> <span style="color:rgb(167,29,93)">==</span> Heads { <span style="color:rgb(150,152,150)">// no leading dot</span>
<span style="color:rgb(0,134,179)">print</span>(<span style="color:rgb(24,54,145)"><span>"</span>This is a head<span>"</span></span>)
}
<span style="color:rgb(167,29,93)">if</span> <span style="color:rgb(167,29,93)">self</span> <span style="color:rgb(167,29,93)">==</span> <span style="color:rgb(167,29,93)">.</span>Tails { <span style="color:rgb(150,152,150)">// leading dot</span>
<span style="color:rgb(0,134,179)">print</span>(<span style="color:rgb(24,54,145)"><span>"</span>This is a tail<span>"</span></span>)
}
}
<span style="color:rgb(167,29,93)">init</span>() {
<span style="color:rgb(167,29,93)">let</span> cointoss <span style="color:rgb(167,29,93)">=</span> arc4random_uniform(<span style="color:rgb(0,134,179)">2</span>) <span style="color:rgb(167,29,93)">==</span> <span style="color:rgb(0,134,179)">0</span>
<span style="color:rgb(167,29,93)">self</span> <span style="color:rgb(167,29,93)">=</span> cointoss <span style="color:rgb(167,29,93)">?</span> <span style="color:rgb(167,29,93)">.</span>Heads <span style="color:rgb(167,29,93)">:</span> Tails <span style="color:rgb(150,152,150)">// mix and match leading dots</span>
}
}</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)">This quirk produces a language inconsistency that can confuse developers and contravenes the guiding <em>Principle of Least Astonishment</em>. We propose to mandate a leading dot. This will bring case mentions into lock-step with the conventions used to reference them outside of enumeration type implementations.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)"><a href="https://gist.github.com/erica/e0b8a3a22ab716a19db4#detail-design" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Detail Design</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)">Under this rule, the compiler will require a leading dot for all case members. The change will not affect other static members, which require fully qualified references from instance methods and infer <code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;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">self</code> from static methods.</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)"><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;padding:16px;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-wrap:normal;word-break:normal"><span style="color:rgb(167,29,93)">enum</span> Coin {
<span style="color:rgb(167,29,93)">case</span> Heads, Tails
<span style="color:rgb(167,29,93)">static</span> <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">doSomething</span>() { <span style="color:rgb(0,134,179)">print</span>(<span style="color:rgb(24,54,145)"><span>"</span>Something<span>"</span></span>) }
<span style="color:rgb(167,29,93)">static</span> <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">staticFunc</span>() { doSomething() } <span style="color:rgb(150,152,150)">// does not require leading dot</span>
<span style="color:rgb(167,29,93)">static</span> <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">staticFunc2</span>() { <span style="color:rgb(167,29,93)">let</span> foo <span style="color:rgb(167,29,93)">=</span> <span style="color:rgb(167,29,93)">.</span>Tails } <span style="color:rgb(150,152,150)">// requires leading dot</span>
<span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">instanceFunc</span>() { <span style="color:rgb(167,29,93)">self</span><span style="color:rgb(167,29,93)">.</span><span style="color:rgb(167,29,93)">dynamicType</span><span style="color:rgb(167,29,93)">.</span>doSomething() } <span style="color:rgb(150,152,150)">// requires full qualification</span>
<span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">otherFunc</span>() { <span style="color:rgb(167,29,93)">if</span> <span style="color:rgb(167,29,93)">self</span> <span style="color:rgb(167,29,93)">==</span> <span style="color:rgb(167,29,93)">.</span>Heads <span style="color:rgb(167,29,93)">...</span> } <span style="color:rgb(150,152,150)">// requires leading dot, also initializers</span>
<span style="color:rgb(150,152,150)">/// ...</span>
} </pre></div><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)"><a href="https://gist.github.com/erica/e0b8a3a22ab716a19db4#alternatives-considered" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Alternatives Considered</h2><div style="margin-top:0px;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255);margin-bottom:0px!important">Other than leaving the status quo, the language could force instance members to refer to cases using a fully qualified type, as with other static members.</div></div></div></div></div><span class="">_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></span></div></blockquote></div><br></div></div><br>_______________________________________________<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/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>