<div dir="ltr">Hi Jordan,<div><br></div><div>Yes implementing `==` will patch the example I gave, but it isn't a general solution. Consider this variation:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">class Static: Equatable {<br> class One: Static {}<br> static let one = One()<br> class Two: Static {}<br> static let two = Two()<br> func e() -> String {<br> switch self {<br> case Static.one:<br> return "one"<br> case Static.two :<br> return "two"<br> default:<br> return "unknown"<br> }<br> }<br>}<br>func ==(lhs: Static, rhs: Static) -> Bool {<br> return lhs === rhs<br>}<br><br></blockquote>Still needs the type qualifiers :(.</div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"> -- Howard.<br></div></div>
<br><div class="gmail_quote">On 17 February 2016 at 11:31, Jordan Rose <span dir="ltr"><<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</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"><div>That works just as well outside the enum:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>enum Enum {</div><div> case one</div><div> case two</div><div>}</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div>func e(value: Enum) -> String {</div></div><div><div> if value == .one {</div></div><div><div> return "one"</div></div><div><div> } else if value == .two {</div></div><div><div> return "two"</div></div><div><div> }</div></div><div><div> return "unkown"</div></div><div><div>}</div></div></blockquote><div><br></div><div>Here it's looking in the contextual type provided by ==. The enum synthesizes a function along the lines of</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>func ==(a: Enum, b: Enum)</div></blockquote><div><br></div>This doesn't work for === because you can compare identity across class types.<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>func ===(a: AnyObject, b: AnyObject)</div></blockquote><div><div><div><br></div><div>Jordan<br><div><br></div><br><div><blockquote type="cite"><div>On Feb 16, 2016, at 16:20, Howard Lovatt <<a href="mailto:howard.lovatt@gmail.com" target="_blank">howard.lovatt@gmail.com</a>> wrote:</div><br><div><div dir="ltr">@Jordan,<div><br></div><div>You said "<span style="font-size:12.8px">I don't think we want to change or overload that rule to also look up static members of the </span><i style="font-size:12.8px">enclosing lexical context type</i><span style="font-size:12.8px"> (i.e. the type of 'self')." </span>But you do already; inside an enum, e.g.:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">enum Enum {<br> case one<br> case two<br> func e() -> String {<br> if self == .one {<br> return "one"<br> } else if self == .two {<br> return "two"<br> }<br> return "unkown"<br> }<br>}</blockquote><div><p>Compared to inside a static:<br></p>
</div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">class Static {<br> class One: Static {}<br> static let one = One()<br> class Two: Static {}<br> static let two = Two()<br> func e() -> String {<br> if self === Static.one {<br> return "one"<br> } else if self === Static.two {<br> return "two"<br> }<br> return "unknown"<br> }<br>}</blockquote><div><p><span>Why so different? The enum version looks inside the type `Enum` to find `.one` and `.two`, but the static version doesn't look inside the type `Static` and hence you have to say `Static.one` and `Static.two` :(</span></p></div></div><div class="gmail_extra"><br clear="all"><div><div> -- Howard.<br></div></div>
<br><div class="gmail_quote">On 16 February 2016 at 05:40, Jordan Rose 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"><div><div><br><div><blockquote type="cite"><div>On Feb 15, 2016, at 8:58, Erica Sadun via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div style="word-wrap:break-word"><div>While discussing leading enumeration dot prefixes, Howard Lovatt asked why static properties and methods could not be inferred via a dot prefix as well. Based on Swift-Evolution conventions, here's a new thread to discuss this idea: specifically, would it be beneficial (and/or possible) to use dot prefixes to denote static member references in instance member code.</div><div><br></div><div>Howard wrote:</div><div><br></div><div></div><blockquote type="cite"><div><span style="font-family:Palatino-Roman">+1 for the proposal, it is wierd to use `.` in some places and not others. </span><div style="font-family:Palatino-Roman"><br></div><div style="font-family:Palatino-Roman">On the subject of static methods and properties in general, would it be possible for `.name` to be a reference to a static and `name` a reference to an instance? EG:</div><div style="font-family:Palatino-Roman"><br></div><div><font face="Menlo"> .name = x // static</font></div><div><font face="Menlo"> name = x // instance</font></div><div><font face="Menlo"> x = name + .name // instance + static</font></div><div><font face="Menlo"> r = name ... .name // instance ... static, needs space</font></div><div><font face="Menlo"> r = name...Type.name // Can still qualify with type name</font></div></div><div style="font-family:Palatino-Roman"><br></div></blockquote><div><br></div><span>Under the current system, you must explicitly name or derive a type to access static members from instance member implementations.</span><div style="font-size:8px"><br style="font-size:8px"></div><div style="font-size:8px"><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">struct</span> MyStruct {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">static</span> <span style="color:#bb2ca2">func</span> staticMember() {}</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:21px"> <br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">func</span> instanceMember() {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="color:rgb(0,132,0)"> // name a type</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"> <span style="color:rgb(79,129,135)">MyStruct</span>.<span style="color:rgb(49,89,93)">staticMember</span>() <span style="color:rgb(0,132,0)">// works</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="color:rgb(0,132,0)"><br></span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="color:rgb(0,132,0)"> // derive a type</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span> </span>self<span>.</span>dynamicType<span>.</span><span style="color:#31595d">staticMember</span><span>() </span><span style="color:#008400">// works</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"> </div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span> </span>// error: value of tuple type '()' has no member 'staticMember'</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span> </span>// does not work</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"> .staticMember()</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">}</div><div style="font-size:8px"><br style="font-size:8px"></div><div style="font-size:12px">Using dot prefixes for static member access:</div></div><div><br></div><div>* Has precedent in enumeration members</div><div>* Would streamline Swift code</div><div>* Is visually differentiated from `self`-prefixed instance member references</div><div><br></div><div>What are your thoughts, both technical and philosophical, on a change like this? Thanks in advance for your feedback.</div></div></div></blockquote><br></div></div></div><div>Dot-prefixed member expressions are currently looked up as static members of the<i> contextual type,</i> which lets you do things like this:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>import AppKit</div><div>let colors: [NSColor] = [.redColor(), .blueColor()]</div></blockquote><br><div>I don't think we want to change or overload that rule to also look up static members of the <i>enclosing lexical context type</i> (i.e. the type of 'self').</div><div><br></div><div>Jordan</div></div><br>_______________________________________________<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>
</div></blockquote></div><br></div></div></div></div></blockquote></div><br></div>