<html><body><div>Comments inline.<br></div><div><br>Am 13. Mai 2016 um 09:31 schrieb "Vladimir.S via swift-evolution" &lt;swift-evolution@swift.org&gt;:<br><br><div><blockquote type="cite"><div class="msg-quote"><div class="_stretch"><span class="body-text-content">I don't feel that InvariantSelf reflects the fact that class conformed to <br>protocol with `f()-&gt;InvariantSelf` requirement will actually return<br>'self or some base class'. Especially when this `InvariantSelf` means <br>`exactly this concrete static type name` inside type declaration.<br><br>Probably the better name is BaseType (BaseSelf, ThisType.. #Type ?)<br><br>But actually I don't fully understand how this would work in generic functions:<br><br>protocol A {<br> func g()-&gt;StaticSelf<br>}<br><br>class B: A {<br> func g()-&gt;StaticSelf {return B()}<br>}<br><br>class C: B {<br>}<br><br><br>func x(a: A ){<br> var xx : A = a.g() // will this work? as g returns *some in hierarchy*<br> print(xx)<br>}</span></div></div></blockquote></div><div><span><br data-mce-bogus="1"></span></div><div><span>StaticSelf within the protocol should just be the protocol, i.e. here A</span></div><div><span><br data-mce-bogus="1"></span></div><div><span>Only for non-protocol types conforming to the protocol StaticSelf becomes equal to the first type where the respective method gets implemented (if we should get abstract classes some day this would mean that for an abstract class conforming to the protocol but not implementing the method a, StaticSelf would still be A).<br data-mce-bogus="1"></span></div><div><span> <br></span><blockquote type="cite"><div class="msg-quote"><div class="_stretch"><span class="body-text-content"><br>func z&lt;T: A&gt;(t: T) {<br> let u = t.g() // will this work?<br> print(u)<br>}</span></div></div></blockquote></div><div><span><br data-mce-bogus="1"></span></div><div><span>This will work as well with static type of `u` being A.</span></div><div><span> <br></span><blockquote type="cite"><div class="msg-quote"><div class="_stretch"><span class="body-text-content"><br>let c = C()<br>z(c)</span></div></div></blockquote></div><div><span><br data-mce-bogus="1"></span></div><div><span>This is of course correct because C conforms to A. The static type of `u` within `z` is A (see comment above). This is independent from the dynamic type of the parameter `t` (which has nothing to do with StaticSelf).</span></div><div><span><br data-mce-bogus="1"></span></div><div><span><br></span><blockquote type="cite"><div class="msg-quote"><div class="_stretch"><span class="body-text-content">x(c)</span></div></div></blockquote></div><div><span><br data-mce-bogus="1"></span></div><div><span>Again `c` conforms to A, so this is correct.</span></div><div><span><br data-mce-bogus="1"></span></div><div><span>-Thorsten</span></div><div><span><br data-mce-bogus="1"></span></div><div><span><br data-mce-bogus="1"></span></div><div><span><br></span><blockquote type="cite"><div class="msg-quote"><div class="_stretch"><span class="body-text-content"><br><br><br><br><br>On 13.05.2016 4:59, Xiaodi Wu via swift-evolution wrote:<br><blockquote type="cite" class="quoted-plain-text">I like the way the motivation for this feature has been explained here. Now</blockquote><blockquote type="cite" class="quoted-plain-text">that the reasoning behind it is evident, I have to say I'm leaning towards</blockquote><blockquote type="cite" class="quoted-plain-text">the "InvariantSelf" name--after all, you describe this feature in the title</blockquote><blockquote type="cite" class="quoted-plain-text">as "an invariant self."</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">On Thu, May 12, 2016 at 7:49 PM, Matthew Johnson via swift-evolution</blockquote><blockquote type="cite" class="quoted-plain-text">&lt;swift-evolution@swift.org &lt;mailto:swift-evolution@swift.org&gt;&gt; wrote:</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Erica Sadun and I have written a proposal are following up the recent</blockquote><blockquote type="cite" class="quoted-plain-text">discussion thread "[RFC] #Self” with a proposal to introduce</blockquote><blockquote type="cite" class="quoted-plain-text">StaticSelf, an invariant Self.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">The recent discussion can be found</blockquote><blockquote type="cite" class="quoted-plain-text">here: <a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/16565" data-mce-href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/16565">http://thread.gmane.org/gmane.comp.lang.swift.evolution/16565</a><br data-mce-bogus="1"></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">The proposal can be found</blockquote><blockquote type="cite" class="quoted-plain-text">here: <a href="https://github.com/anandabits/swift-evolution/blob/static-self/proposals/NNNN-static-self.md" data-mce-href="https://github.com/anandabits/swift-evolution/blob/static-self/proposals/NNNN-static-self.md">https://github.com/anandabits/swift-evolution/blob/static-self/proposals/NNNN-static-self.md</a><br data-mce-bogus="1"></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">We look forward to continuing the discussion. We plan to submit a PR</blockquote><blockquote type="cite" class="quoted-plain-text">in the near future after incorporating your final feedback.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Thanks,</blockquote><blockquote type="cite" class="quoted-plain-text">Matthew</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Introducing StaticSelf, an Invariant Self</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">* Proposal: TBD</blockquote><blockquote type="cite" class="quoted-plain-text">* Authors: Matthew Johnson &lt;<a href="https://github.com/anandabits>" data-mce-href="https://github.com/anandabits>">https://github.com/anandabits&gt;</a>, Erica</blockquote><blockquote type="cite" class="quoted-plain-text">Sadun &lt;<a href="https://github.com/erica>" data-mce-href="https://github.com/erica>">https://github.com/erica&gt;</a><br data-mce-bogus="1"></blockquote><blockquote type="cite" class="quoted-plain-text">* Status: TBD</blockquote><blockquote type="cite" class="quoted-plain-text">* Review manager: TBD</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Introduction</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">This proposal introduces a new keyword that provides consistent</blockquote><blockquote type="cite" class="quoted-plain-text">invariant type semantics in all contexts.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">/The Swift-evolution thread about this topic can be found here: [RFC]</blockquote><blockquote type="cite" class="quoted-plain-text">#Self &lt;<a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/16565>/" data-mce-href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/16565>/">http://thread.gmane.org/gmane.comp.lang.swift.evolution/16565&gt;/</a><br data-mce-bogus="1"></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Motivation</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">The distinction between covariant and non-covariant type references</blockquote><blockquote type="cite" class="quoted-plain-text">come into play when</blockquote><blockquote type="cite" class="quoted-plain-text">conforming non-final classes to protocols. Fixing a protocol</blockquote><blockquote type="cite" class="quoted-plain-text">requirement to a covarying type</blockquote><blockquote type="cite" class="quoted-plain-text">means that a method returning |Self| must be overriden by all</blockquote><blockquote type="cite" class="quoted-plain-text">subclasses in order to return</blockquote><blockquote type="cite" class="quoted-plain-text">the correct, matching type.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">This proposal builds on the covariant construct |Self| accepted</blockquote><blockquote type="cite" class="quoted-plain-text">in SE–0068</blockquote><blockquote type="cite" class="quoted-plain-text">&lt;<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md>" data-mce-href="https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md>">https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md&gt;</a><br data-mce-bogus="1"></blockquote><blockquote type="cite" class="quoted-plain-text">to introduce an invariant type identifier. It enables protocol</blockquote><blockquote type="cite" class="quoted-plain-text">declarations to consistently</blockquote><blockquote type="cite" class="quoted-plain-text">refer to a type that is fixed at compile time. This ensures that</blockquote><blockquote type="cite" class="quoted-plain-text">subclasses can inherit</blockquote><blockquote type="cite" class="quoted-plain-text">protocol implementations without having to re-implement that code at</blockquote><blockquote type="cite" class="quoted-plain-text">each level of</blockquote><blockquote type="cite" class="quoted-plain-text">inheritance.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Under this proposal, a new identifier keyword is fixed in use /at the</blockquote><blockquote type="cite" class="quoted-plain-text">point of protocol conformance/</blockquote><blockquote type="cite" class="quoted-plain-text">to the static type of that construct.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">|class A: MyProtocol|</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">The invariant |StaticSelf| identifier will always refer to |A|,</blockquote><blockquote type="cite" class="quoted-plain-text">unlike |Self|, which is covarying and refers to</blockquote><blockquote type="cite" class="quoted-plain-text">the type of the actual instance. Since multiple inheritance for</blockquote><blockquote type="cite" class="quoted-plain-text">non-protocol types is disallowed,</blockquote><blockquote type="cite" class="quoted-plain-text">this establishes this invariant type identifier with no possibility for</blockquote><blockquote type="cite" class="quoted-plain-text">conflict.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Consider the following example, under the current system:</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">|protocol StringCreatable { static func createWithString(s: String) -&gt;</blockquote><blockquote type="cite" class="quoted-plain-text">Self } extension NSURL: StringCreatable { // cannot conform because</blockquote><blockquote type="cite" class="quoted-plain-text">NSURL is non-final // error: method 'createWithString' in non-final</blockquote><blockquote type="cite" class="quoted-plain-text">class 'NSURL' must return `Self` to conform to protocol 'A' }|</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Introducing a static, invariant version of |Self| permits the desired</blockquote><blockquote type="cite" class="quoted-plain-text">conformance:</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">|protocol StringCreatable { static func createWithString(s: String) -&gt;</blockquote><blockquote type="cite" class="quoted-plain-text">StaticSelf } extension NSURL: StringCreatable { // can now conform</blockquote><blockquote type="cite" class="quoted-plain-text">conform because NSURL is fixed and matches the static // type of the</blockquote><blockquote type="cite" class="quoted-plain-text">conforming construct. Subclasses need not re-implement // NOTE: the</blockquote><blockquote type="cite" class="quoted-plain-text">return type can be declared as StaticSelf *or* as NSURL // they are</blockquote><blockquote type="cite" class="quoted-plain-text">interchangeable static func createWithString(s: String) -&gt; StaticSelf {</blockquote><blockquote type="cite" class="quoted-plain-text">// ... } }|</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Additional Utility</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">The utility of |StaticSelf| is not limited to protocols. A secondary</blockquote><blockquote type="cite" class="quoted-plain-text">use enables code to refer to the lexical context’s current type without</blockquote><blockquote type="cite" class="quoted-plain-text">explicitly mentioning its name. This provides a useful shortcut when</blockquote><blockquote type="cite" class="quoted-plain-text">referencing static type members with especially long names and when</blockquote><blockquote type="cite" class="quoted-plain-text">re-purposing code between types.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">|class StructWithAVeryLongName { static func foo() -&gt; String { // ... }</blockquote><blockquote type="cite" class="quoted-plain-text">func bar() { // ... let s = StaticSelf.foo() // } }|</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Detailed Design</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">This proposal introduces |StaticSelf|, a new keyword that may be used</blockquote><blockquote type="cite" class="quoted-plain-text">in protocols to refer to the invariant static type of a conforming</blockquote><blockquote type="cite" class="quoted-plain-text">construct. |StaticSelf| may also be used in the lexical context of any</blockquote><blockquote type="cite" class="quoted-plain-text">type declaration. In such use, the keyword is identical to spelling out</blockquote><blockquote type="cite" class="quoted-plain-text">the full name of that type.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Impact on existing code</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Being additive, there should be no impact on existing code.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">Alternatives considered</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">The keyword is not fixed at this time. Alternatives that have been</blockquote><blockquote type="cite" class="quoted-plain-text">discussed include |StaticType|, |InvariantSelf|, |SelfType|, or |Type|.</blockquote><blockquote type="cite" class="quoted-plain-text">The community is welcome to bikeshed on the most clear and concise name</blockquote><blockquote type="cite" class="quoted-plain-text">for this keyword.</blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">_______________________________________________</blockquote><blockquote type="cite" class="quoted-plain-text">swift-evolution mailing list</blockquote><blockquote type="cite" class="quoted-plain-text"><a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a> &lt;mailto:swift-evolution@swift.org&gt;</blockquote><blockquote type="cite" class="quoted-plain-text"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br data-mce-bogus="1"></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote><blockquote type="cite" class="quoted-plain-text">_______________________________________________</blockquote><blockquote type="cite" class="quoted-plain-text">swift-evolution mailing list</blockquote><blockquote type="cite" class="quoted-plain-text"><a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br data-mce-bogus="1"></blockquote><blockquote type="cite" class="quoted-plain-text"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br data-mce-bogus="1"></blockquote><blockquote type="cite" class="quoted-plain-text"><br></blockquote>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></span></div></div></blockquote></div></div></body></html>