This continues to forbid use cases that are critical.<br><br>For instance, I am writing a library that vends additional conformances for Float and Double. Any numerics library would need to do the same.<br><br>Your design would eliminate all such libraries, which is a non-starter. I am not sure what defects you are trying to solve with this proposal.<br><div class="gmail_quote"><div dir="ltr">On Sun, Apr 16, 2017 at 17:51 Howard Lovatt &lt;<a href="mailto:howard.lovatt@gmail.com">howard.lovatt@gmail.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>@Brent,</div><div id="m_-2753664487318090949AppleMailSignature"><br></div><div id="m_-2753664487318090949AppleMailSignature">I have updated the proposal to address your concerns, in particular I don&#39;t see that retrospectively adding methods and protocols has been removed it has just had its ugly corners rounded. See revised proposal below particularly the end of section &quot;<span style="background-color:rgba(255,255,255,0)">Retrospectively adding protocols and methods&quot; and new section &quot;Justification&quot;.</span></div><div id="m_-2753664487318090949AppleMailSignature"><br></div><div id="m_-2753664487318090949AppleMailSignature">Hope this convinces you that the change is worthwhile.<br><br>-- Howard.</div><div id="m_-2753664487318090949AppleMailSignature"><br></div><div id="m_-2753664487318090949AppleMailSignature">====================================</div><div id="m_-2753664487318090949AppleMailSignature"><br></div><div id="m_-2753664487318090949AppleMailSignature"><p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"># Proposal: Split extension usage up into implementing methods and adding methods and protocols retrospectively</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## Revision history</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">| Version | Date               | Comment       |</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">|---------|--------------|--------------|</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">| Draft 1   | 11 April 2017 | Initial version |</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">| Draft 2  | 13 April 2017 | Added support for post-hoc conformance to a protocol - replaced static final extensions with final extensions |</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">| Draft 3 | 17 April 2017 | Added justification section |</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## Introduction</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Currently extension methods are confusing because they have different dispatch rules for the same calling syntax. EG:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    public protocol P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func mP() -&gt; String</span></p></div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature">
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">     }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    extension P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func mP() -&gt; String { return &quot;P.mP&quot; }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func mE() -&gt; String { return &quot;P.mE&quot; }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    struct S: P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func mP() -&gt; String { return &quot;S.mP&quot; }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func mE() -&gt; String { return &quot;S.mE&quot; }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    let s = S()</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    s.mP() // S.mP as expected</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    s.mE() // S.mE as expected</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    let p: P = s // Note: s now typed as P</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    p.mP() // S.mP as expected</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    p.mE() // P.mE unexpected!</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Extension methods can also cause compatibility problems between modules, consider:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">In Module A</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    extension Int: P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m() -&gt; String { print(&quot;A.m&quot;) }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">In Module B</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    extension Int: P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m() -&gt; String { print(&quot;B.m&quot;) }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">In Module C</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    import A</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    import B // Should this be an error</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    let i = 0</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    i.m() // Should it return A.m or B.m?</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
</div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature"><p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">This proposal cures the above two problems by separating extension methods into two seperate use cases: implementations for methods and adding methods and protocols retrospectively. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## Implementing methods</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">If the extension is in the same file as the protocol/struct/enum/class declaration then it implements the methods and is dispatched using a Vtable. EG:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">File P.swift</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    protocol/struct/enum/class P {</span></p></div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature">
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        // func m() not declared in type since it is added by the extension, under this proposal it is an error to include a declaration in a type *and* in an extension</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    extension P {</span></p>
</div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature"><p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m() { print(&quot;P.m&quot;) } // m is added to the protocol/struct/enum/class declaration</span></p></div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature">
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Same or other file</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    struct S: P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        override func m() { print(&quot;S.m&quot;) } // Note override required because m already has an implementation from the extension</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    let p: P = S() // Note typed as P</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    p.m() // Now prints S.m as expected </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Extensions in the same file as the declaration can have any access, can be final, and can have where clauses and provide inheritable implementations. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
</div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature"><p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">In a protocol at present there is a difference in behaviour between a protocol that declares a method that is then implemented in an extension and a protocol that just has the method implemented in an extension and no declaration. This situation only applies to protocols, for structs/enumerated/classes you cannot declare in type and implement in extensions. The proposal unifies the behaviour of protocol/struct/enum/class with extensions and prevents the error of a minor typo between the protocol and extension adding two methods instead of generating an error.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">The implementation needed to achieve this proposal is that a value instance typed as a protocol is copied onto the heap, a pointer to its Vtable added, and it is passed as a pointer. IE it becomes a class instance. No change needed for a class instance typed as a protocol. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## Retrospectively adding protocols and methods</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">A new type of extension is proposed, a &quot;final extension&quot;, which can be either in or outside the file in which the protocol/struct/enum/class declaration is in. EG:</span></p></div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature">
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    protocol P2 {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m2P()</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    final extension S: P2 { // Note extension marked final</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m2P() { print(&quot;SP2.m2P&quot;) } // Implicitly final, completely implements P2</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m2E() { print(&quot;SP2.m2E&quot;) } // Implicitly final, not an existing method</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Which are called as any other method would be called:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    let s = S()</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    s.m2P() // Prints SP2.m2P</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    s.m2E() // Prints SP2.m2E</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
</div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature"><p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">A method added by a final extension is is implicitly final, as the name would suggest, and cannot be overridden. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Notes:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  1. If the final extension adds a method, e.g. m2E, that method cannot already exist. IE a final extension cannot override an existing method or implement a protocol declared method that lacks an implementation unless it also adds the protocol.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  2. If the final extension adds a protocol then it must implement all the methods in that protocol that are not currently implemented.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  3. If the final extension is outside of the file in which the protocol/struct/enum/class declaration is in then the extension and the methods can only have fileprivate or internal access. This prevents retrospective extensions from numerous modules clashing, since they are not exported outside of the module. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">When a type is extended inside a module with a final extension the extension is not exported. For example:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    final extension Int: P2 {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m2P() { print(&quot;Int.m2P&quot;) } </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">If an exported function uses Int, e.g.:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    public func f(_ x: Int) -&gt; Int {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        x.m2P()</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        return x</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Then when used in an external module both the input Int and the output Int are not extended with P2. However as the Int goes into f it gains P2 conformance and when it leaves it looses P2 conformance. Thus inside and outside the module the behaviour is easily understood and consistent and doesn&#39;t clash with other final extensions in other modules.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Taking the above example further an Int with P2 conformance is required by the user of a library; then it can simply and safely be provided, e.g.:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    public class P2Int: P2 {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        var value = 0</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m2P() { print(&quot;Int.m2P&quot;) }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">This type, P2Int, is easy to write, one line longer than a final extension, and can easily be used as both a P2 and an Int and does not clash with another Int extension from another module.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## Justification </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">The aim of Swift is nothing more than dominating the world. Using the current, April 2017, <a href="https://www.tiobe.com/tiobe-index/" target="_blank"><span style="font-size:17pt;color:rgb(228,175,10)">https://www.tiobe.com/tiobe-index/</span></a> index of job adverts for programmers the languages that are in demand are: Java 15.568%, C 6.966%, C++ 4.554%, C# 3.579%, Python 3.457%, PHP 3.376%, Visual Basic .NET 3.251%, JavaScript 2.851%, Delphi/Object Pascal 2.816%, Perl 2.413%, Ruby 2.310%, and Swift 2.287%. So Swift at 12th is doing very well for a new language and is already above Objective-C at 14th. However there is obviously a long way to go and the purpose of this proposal is to help with this climb.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">A characteristic of many of the languages above Swift in the Tiobe Index is that they have major third party libraries; for some languages they are almost defined by their third part libraries, e.g. Ruby for Rails. A major part of this proposal is to make extensions safe when using multiple libraries from different venders. In particular final extensions are not exported.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">As part of Swift&#39;s goal of world domination is that it is meant to be easy to learn by a process of &quot;successive disclosure&quot;. The current inconsistent behaviour of protocols and extensions hinders this process and is a common gotcha for newbies. This proposal eliminates that problem also.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Extensions are not new in languages, they are part of the .NET languages for example. Since .NET popularised extensions they have been discussed by other language communities, particularly Java and Scala, and in the academic community (normally termed the Expression Problem) however they have not proved popular because of the problems they cause. Nearly all languages have a strong bias towards keeping the language small and simple and trade of the advantages of a feature against the disadvantages and the feature only makes it into the language if it offers many advantages, has few disadvantages, and is not heavily overlapping with other features. This keeping it small and simple test is what extensions have failed in other languages.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Experience from .NET can however be used to improve extensions. There is some excellent advice <a href="https://blogs.msdn.microsoft.com/vbteam/2007/03/10/extension-methods-best-practices-extension-methods-part-6/" target="_blank">https://blogs.msdn.microsoft.com/vbteam/2007/03/10/extension-methods-best-practices-extension-methods-part-6/</a> written by the VB .NET team when they added extensions to VB .NET. The best-practice advice can be summarised by the following quotes from the reference:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  0. &quot;In most real world applications these suggestions [the rest of the suggestions] can (and quite frankly should!) be completely ignored.&quot; This is an important observations, in your own code that is not intended for reuse; go for it, use extensions. The proposal importantly still allows this style of programming and in fact improves it by adding consistent behaviour and syntax between protocols/structs/enumerated/classes.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"> 1. &quot;Read the .NET Framework Class Library Design Guidelines.&quot; The equivalent for Swift is lacking at this stage. Probably because third party libraries are rare.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  2. &quot;Be wary of extension methods.&quot; This recommendation is formalised in the proposal by limiting final extensions to be fileprivate or internal.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  3. &quot;Put extension methods into their own namespace.&quot; This recommendation is formalised in the proposal by limiting final extensions to be fileprivate or internal.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  4. &quot;Think twice before extending types you don’t own.&quot; </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  5. &quot;Prefer interface extensions over class extensions.&quot; Translation to Swift terminology provide default implementations for protocol methods. The proposal encourages this by eliminating a major gotcha with the current implementation, namely the proposal always dispatches via a Vtable to give consistent behaviour.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  6. &quot;Be as specific with the types you extend as possible.&quot; Translation to Swift terminology provide default implementations for protocol methods that extend other  protocols if there is a more specific behaviour that is relevent. The proposal encourages this by eliminating a major gotcha with the current implementation, namely the proposal always dispatches via a Vtable to give consistent behaviour.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">The proposal formalises these best practices from .NET whilst increasing consistence and without loosing the ability to use extensions heavily in your own one-off code to allow for rapid development. Most of the best practices are for better libraries, particularly third party, which is an important area for future Swift growth onto the server side. This proposal actively encourages this transition to large formal server side code without loosing the free wheeling nature of app code.</span></p></div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature">
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## Possible future work (not part of this proposal)</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">This proposal will naturally allow bodies to be added to protocols directly rather than via an extension, since under the proposal the extension adds the declaration to the type so it is a small step to allow the protocol methods to have an implementation. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">In an opposite sense to the above adding bodies to protocols, extensions could be allowed to add method declarations without bodies to protocols. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">The two above future work proposals, if both added, would add symmetry to where declarations and bodies may appear for protocols. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## In summary.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">The proposal formalises the split use of extensions into their two uses: implementing methods and post-hoc adding protocols and methods. Syntax is added that clarifies the two use cases, the former are termed extensions and must be in the same file as the type is declared, and the latter are termed final extensions and can be in any file, however if they are not in the type&#39;s file the they can only have fileprivate or internal access.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Note the distinction between an extension in the same file and in a separate file is consistent with the philosophy that there is special status to the same file as proposed for private in <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0169-improve-interaction-between-private-declarations-and-extensions.md" target="_blank"><span style="font-size:17pt;color:rgb(228,175,10)">https://github.com/apple/swift-evolution/blob/master/proposals/0169-improve-interaction-between-private-declarations-and-extensions.md</span></a>.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
</div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature"><p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">===================================================</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">#Proposal: Split extension usage up into implementing methods and adding methods and protocols post-hoc</span></p></div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature">
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Draft 2 (Added support for post-hoc conformance to a protocol - replaced static final extensions with final extensions)</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## Introduction</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
</div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature"><p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Currently extension methods are confusing because they have different dispatch rules for the same calling syntax. EG:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    public protocol P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func mP() -&gt; String</span></p></div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature">
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">     }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    extension P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func mP() -&gt; String { return &quot;P.mP&quot; }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func mE() -&gt; String { return &quot;P.mE&quot; }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    struct S: P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func mP() -&gt; String { return &quot;S.mP&quot; }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func mE() -&gt; String { return &quot;S.mE&quot; }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    let s = S()</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    s.mP() // S.mP as expected</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    s.mE() // S.mE as expected</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    let p: P = s // Note: s now typed as P</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    p.mP() // S.mP as expected</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    p.mE() // P.mE unexpected!</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Extension methods can also cause compatibility problems between modules, consider:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">In Module A</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    extension Int: P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m() -&gt; String { print(&quot;A.m&quot;) }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">In Module B</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    extension Int: P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m() -&gt; String { print(&quot;B.m&quot;) }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">In Module C</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    import A</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    import B // Should this be an error</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    let i = 0</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    i.m() // Should it return A.m or B.m?</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">This proposal cures the above two problems by separating extension methods into two seperate use cases: implementations for methods and adding methods and protocols post-hoc. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## Implementing methods</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">If the extension is in the same file as the protocol/struct/class declaration then it implements the methods and is dispatched using a Vtable. EG:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">File P.swift</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    protocol/struct/class P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        // func m() not declared in type since it is added by the extension, under this proposal it is an error to include a declaration in a type *and* in an extension</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    extension P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m() { print(&quot;P.m&quot;) } // m is added to the protocol/struct/class declaration</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Same or other file</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    struct S: P {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        override func m() { print(&quot;S.m&quot;) } // Note override required because m already has an implementation from the extension</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    let p: P = S() // Note typed as P</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    p.m() // Now prints S.m as expected </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Extensions in the same file as the declaration can have any access, can be final, and can have where clauses and provide inheritable implementations. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">In a protocol at present there is a difference in behaviour between a protocol that declares a method that is then implemented in an extension and a protocol that just has the method implemented in an extension and no declaration. This situation only applies to protocols, for structs and classes you cannot declare in type and implement in extensions. The proposal unifies the behaviour of protocol/struct/class with extensions and prevents the error of a minor typo between the protocol and extension adding two methods instead of generating an error.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">The implementation needed to achieve this is that a value instance typed as a protocol is copied onto the heap, a pointer to its Vtable added, and it is passed as a pointer. IE it becomes a class instance. No change needed for a class instance typed as a protocol. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## Post-hoc adding protocols and methods</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">A new type of extension is proposed, a &quot;final extension&quot;, which can be either in or outside the file in which the protocol/struct/class declaration is in. EG:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    protocol P2 {</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m2P()</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    final extension S: P2 { // Note extension marked final</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m2P() { print(&quot;SP2.m2P&quot;) } // Implicitly final, completely implements P2</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">        func m2E() { print(&quot;SP2.m2E&quot;) } // Implicitly final, not an existing method</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    }</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Which are called as any other method would be called:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    let s = S()</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    s.m2P() // Prints SP2.m2P</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">    s.m2E() // Prints SP2.m2E</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
</div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature"><p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">A method added by a final extension is is implicitly final, as the name would suggest, and cannot be overridden. </span></p></div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature">
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">If the final extension:</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  1. Adds a method, e.g. m2E, that method cannot already exist. IE a final extension cannot override an existing method or implement a protocol declared method that lacks an implementation unless it also post-hoc adds the protocol.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  2. Adds a protocol then it must implement all the methods in that protocol that are not currently implemented.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">  3. Is outside of the file in which the protocol/struct/class declaration is in then the extension and the methods can only have fileprivate or internal access. This prevents post-hoc extensions from numerous modules clashing, since they are not exported outside of the module. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## Possible future work (not part of this proposal)</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">This proposal will naturally allow bodies to be added to protocols directly rather than via an extension, since under the proposal the extension adds the declaration to the type so it is a small step to allow the protocol methods to have an implementation. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">In an opposite sense to the above adding bodies to protocols, extensions could be allowed to add method declarations without bodies to protocols. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">The two above future work proposals, if both added, would add symmetry to where declarations and bodies may appear for protocols. </span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">## In summary.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">The proposal formalises the split use of extensions into their two uses: implementing methods and post-hoc adding protocols and methods. Syntax is added that clarifies the two use cases, the former are termed extensions and must be in the same file as the type is declared, and the latter are termed final extensions and can be in any file, however if they are not in the type&#39;s file the they can only have fileprivate or internal access.</span></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69);min-height:20.3px"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"></span><br></p>
<p style="margin:0px;line-height:normal;font-family:&#39;.SF UI Text&#39;;color:rgb(69,69,69)"><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt">Note the distinction between an extension in the same file and in a separate file is consistent with the philosophy that there is special status to the same file as proposed for private in <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0169-improve-interaction-between-private-declarations-and-extensions.md" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/0169-improve-interaction-between-private-declarations-and-extensions.md</a>.</span></p><div><span style="font-family:&#39;.SFUIText&#39;;font-size:17pt"><br></span></div></div></div><div dir="auto"><div id="m_-2753664487318090949AppleMailSignature"></div><div id="m_-2753664487318090949AppleMailSignature"><br></div><div id="m_-2753664487318090949AppleMailSignature">====================================</div></div><div dir="auto"><div><br>On 14 Apr 2017, at 8:17 am, Brent Royal-Gordon &lt;<a href="mailto:brent@architechies.com" target="_blank">brent@architechies.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><blockquote type="cite"><span>On Apr 13, 2017, at 3:10 PM, Howard Lovatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>I don&#39;t see that retroactive conformance needs to be exportable. If it is exported then you cannot prevent clashes from two modules, this is a known problem in C#. Because of this and other problems with C# extensions, this style of extension were rejected by other language communities (notably Java and Scala). </span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>A better alternative for export is a new class that encapsulates the standard type but with added methods for the protocol to be added. This way there is no clash between modules. EG:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>    public protocol P {</span><br></blockquote><blockquote type="cite"><span>        func m() -&gt; String</span><br></blockquote><blockquote type="cite"><span>    }</span><br></blockquote><blockquote type="cite"><span>    public class PInt: P {</span><br></blockquote><blockquote type="cite"><span>        var value = 0</span><br></blockquote><blockquote type="cite"><span>        func m() -&gt; String { return &quot;PI.m&quot; }</span><br></blockquote><blockquote type="cite"><span>    }</span><br></blockquote><span></span><br><span>Howard, this would be very source-breaking and would fail to achieve fundamental goals of Swift&#39;s protocol design. Removing retroactive conformance is no more realistic than removing Objective-C bridging—another feature which introduces various ugly edge cases and tricky behaviors but is also non-negotiable.</span><br><span></span><br><span>-- </span><br><span>Brent Royal-Gordon</span><br><span>Architechies</span><br><span></span><br></div></blockquote></div></blockquote></div>