<div dir="ltr"><span style="font-size:13px">I can&#39;t reply directly to all the points in this thread, but I will just say that there are ways of emulating protected in the language as it exists today:</span><div style="font-size:13px"><br></div><div style="font-size:13px">If the goal is to have functions that can be overridden by subclasses without being called by external classes, you can create a parameter of a public type with a private initializer and use it in all &quot;protected&quot; member functions. No external code will be able to call such functions because they cannot create an argument of the correct type.</div><div style="font-size:13px"><br></div><div style="font-size:13px">If external classes need to both override and call the &quot;protected&quot; members, you&#39;ll need to arrange to pass the key to derived classes which you can do by taking a function as an argument to the base class initializer. The base class then calls the function to pass the key to the derived class. This way only parties that are able to control the function passed to the initializer are able to obtain the key necessary to call the &quot;protected&quot; member functions.</div><div style="font-size:13px"><br></div><div style="font-size:13px">You can produce a lot of different access control mechanisms using the same idea.</div><div style="font-size:13px"><br></div><div style="font-size:13px">I&#39;ll write something up at <a href="http://www.callionica.com/developer" target="_blank">http://www.callionica.com/developer</a> in the next day or so</div><div style="font-size:13px"><br></div><div style="font-size:13px">-- Callionica</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 29, 2016 at 2:55 PM, Vanderlei Martinelli via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Thank you all for your comments. :-)</div><div><br></div><div>Well... My goal is to keep the thing really simple and do not start a new &quot;OOP x POP&quot; (or &quot;something&quot; x &quot;other thing&quot;) war.</div><div><br></div><div>&quot;Protected&quot; access level is not a new concept at all (except for the Swift language), so I did not propose anything preposterous.<br></div><div><br></div><div>Of course in the Swift of my dreams we also have &quot;abstract&quot; access level modifier, &quot;protected&quot; access level, *real* &quot;private&quot; access level and &quot;file&quot; access level modifier (along with many, many other things, of course). But this proposal is not about this. It is only about include the &quot;protected&quot; access level.</div><div><br></div><div>There is, however, something that I need to get off my chest: I really would like to have the freedom to go to the depths with protocols as well with classes. I work in real apps everyday that uses Cocoa frameworks (based on classes) and these apps must be shipped and I like them well written. Maybe am I insane for proposing a better support for classes in Swift? If so, this explains why every time I suggest better support for classes in Swift there is an endless discussion and someone proclaims the death of OOP and is it. OK... Maybe someday we will not have more classes in Swift. Until there: the current language status is the best way to handle OOP in Swift? Or is there a better way? I think there is.</div><div><br></div><div><br></div><div>Regards,</div><div><br></div><div>Vanderlei Martinelli</div><div><br></div><div><br></div><div><br></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><br></div><div><br></div></div>
</blockquote></div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Sat, May 28, 2016 at 7:52 PM, Vanderlei Martinelli <span dir="ltr">&lt;<a href="mailto:vmartinelli@alecrim.com" target="_blank">vmartinelli@alecrim.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello.<div><br></div><div><br></div><div>This is the first draft. I&#39;d like to know your opinion about it.</div><div><br></div><div>(I know that this subject could have been discussed before. If so, please indicate me the correct thread to follow and interact.)</div><div><br></div><div><br></div><div>Regards,</div><div><br></div><div>Vanderlei Martinelli</div><div><br></div><div><br></div><div>---</div><div><br></div><div><br></div><div><h2 style="margin:20px 0px 10px;padding:0px;font-size:24px;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204,204,204);color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif">Introduction</h2><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">Protected access level will enable entities to be used within the container type and by derived types only.</p><h2 style="margin:20px 0px 10px;padding:0px;font-size:24px;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204,204,204);color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif">Motivation</h2><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">Today Swift has three access levels (<code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">public</code>, <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">internal</code> and <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">private</code>), but lacks a way to describe a member that can be only visible to its type or derived types.</p><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">A common case is the <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">UIView</code> from UIKit. Many developers are tempted to make this call:</p><div style="color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px"><pre style="margin-top:0.5em;margin-bottom:0.5em;background-color:rgb(245,242,240);border:1px solid rgb(204,204,204);font-size:13px;line-height:1.5;overflow:auto;padding:1em 1em 1em 3.8em;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal"><code style="margin:0px;padding:0px;border:none;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-image:none;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal;line-height:1.5">view<span style="color:rgb(153,153,153)">.</span><span style="color:rgb(221,74,104)">layoutSubviews</span><span style="color:rgb(153,153,153)">(</span><span style="color:rgb(153,153,153)">)</span><span style="width:3em;letter-spacing:-1px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(153,153,153)"><span style="display:block"></span></span></code></pre></div><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">The documentation says: &quot;You should not call this method directly. If you want to force a layout update, call the <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">setNeedsLayout</code>method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">layoutIfNeeded</code> method.&quot;</p><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">But yes, you should call this method directly if you are subclassing the view and needs to perform additional layout to its subviews (&quot;subclasses can override this method as needed&quot;):</p><div style="color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px"><pre style="margin-top:0.5em;margin-bottom:0.5em;background-color:rgb(245,242,240);border:1px solid rgb(204,204,204);font-size:13px;line-height:1.5;overflow:auto;padding:1em 1em 1em 3.8em;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal"><code style="margin:0px;padding:0px;border:none;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-image:none;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal;line-height:1.5"><span style="color:rgb(0,119,170)">public</span> <span style="color:rgb(0,119,170)">override</span> <span style="color:rgb(0,119,170)">func</span> <span style="color:rgb(221,74,104)">layoutSubviews</span><span style="color:rgb(153,153,153)">(</span><span style="color:rgb(153,153,153)">)</span> <span style="color:rgb(153,153,153)">{</span>
    <span style="color:rgb(112,128,144)">// We are calling the super method directly here.</span>
    <span style="color:rgb(0,119,170)">super</span><span style="color:rgb(153,153,153)">.</span><span style="color:rgb(221,74,104)">layoutSubviews</span><span style="color:rgb(153,153,153)">(</span><span style="color:rgb(153,153,153)">)</span>
    
    <span style="color:rgb(112,128,144)">// Do more adjustments to this view&#39;s subviews...</span>
<span style="color:rgb(153,153,153)">}</span><span style="width:3em;letter-spacing:-1px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(153,153,153)"><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span></span></code></pre></div><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">So, yes, we can call this method directly when subclassing, but the Swift compiler will not prevent you from do this when not subclassing or from any other foreign class. It will not even issue a warning.</p><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">In Objective-C problems like this are usually &quot;solved&quot; my adding a kind of &quot;protected&quot; header (<code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">.h</code>) that is intended to be included only when the developer is subclassing. In Swift we do not have headers, but we have the new access level model. So, if the declaration of this method was...</p><div style="color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px"><pre style="margin-top:0.5em;margin-bottom:0.5em;background-color:rgb(245,242,240);border:1px solid rgb(204,204,204);font-size:13px;line-height:1.5;overflow:auto;padding:1em 1em 1em 3.8em;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal"><code style="margin:0px;padding:0px;border:none;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-image:none;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal;line-height:1.5">protected <span style="color:rgb(0,119,170)">func</span> <span style="color:rgb(221,74,104)">layoutSubviews</span><span style="color:rgb(153,153,153)">(</span><span style="color:rgb(153,153,153)">)</span><span style="width:3em;letter-spacing:-1px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(153,153,153)"><span style="display:block"></span></span></code></pre></div><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">... no one outside the class or derived classes would be allowed to call this method directly.</p><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">Of course, there are other cases in the Cocoa frameworks and there are many other cases when we are developing software in Swift that the <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">protected</code> access level would be very usefull.</p><h2 style="margin:20px 0px 10px;padding:0px;font-size:24px;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204,204,204);color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif">Proposed solution</h2><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">Create the <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">protected</code> access level.</p><h2 style="margin:20px 0px 10px;padding:0px;font-size:24px;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204,204,204);color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif">Detailed design</h2><h3 style="margin:20px 0px 10px;padding:0px;font-size:18px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif">Reference Types (classes)</h3><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">When declarated by a <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">class</code> the protected member will be visible to the class itself and all the derived classes.</p><div style="color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px"><pre style="margin-top:0.5em;margin-bottom:0.5em;background-color:rgb(245,242,240);border:1px solid rgb(204,204,204);font-size:13px;line-height:1.5;overflow:auto;padding:1em 1em 1em 3.8em;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal"><code style="margin:0px;padding:0px;border:none;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-image:none;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal;line-height:1.5"><span style="color:rgb(112,128,144)">// BaseClass.swift</span>
<span style="color:rgb(0,119,170)">public</span> <span style="color:rgb(0,119,170)">class</span> <span>BaseClass</span> <span style="color:rgb(153,153,153)">{</span>
    <span style="color:rgb(0,119,170)">public</span> <span style="color:rgb(221,74,104)">protected</span><span style="color:rgb(153,153,153)">(</span><span style="color:rgb(0,119,170)">set</span><span style="color:rgb(153,153,153)">)</span> <span style="color:rgb(0,119,170)">var</span> x <span style="color:rgb(166,127,89);background-color:rgba(255,255,255,0.498039)">=</span> <span style="color:rgb(153,0,85)">20</span>
    protected <span style="color:rgb(0,119,170)">let</span> y <span style="color:rgb(166,127,89);background-color:rgba(255,255,255,0.498039)">=</span> <span style="color:rgb(153,0,85)">10</span>
    
    protected <span style="color:rgb(0,119,170)">func</span> <span style="color:rgb(221,74,104)">doSomething</span><span style="color:rgb(153,153,153)">(</span><span style="color:rgb(153,153,153)">)</span> <span style="color:rgb(153,153,153)">{</span>
        <span style="color:rgb(112,128,144)">// ...</span>
    <span style="color:rgb(153,153,153)">}</span>
<span style="color:rgb(153,153,153)">}</span>

<span style="color:rgb(112,128,144)">// DerivedClass.swift</span>
<span style="color:rgb(0,119,170)">public</span> <span style="color:rgb(0,119,170)">class</span> <span>DerivedClass</span><span style="color:rgb(153,153,153)">:</span> <span style="color:rgb(102,153,0)">BaseClass</span> <span style="color:rgb(153,153,153)">{</span>
    protected <span style="color:rgb(0,119,170)">override</span> <span style="color:rgb(221,74,104)">doSomething</span><span style="color:rgb(153,153,153)">(</span><span style="color:rgb(153,153,153)">)</span> <span style="color:rgb(153,153,153)">{</span>
        <span style="color:rgb(0,119,170)">self</span><span style="color:rgb(153,153,153)">.</span>x <span style="color:rgb(166,127,89);background-color:rgba(255,255,255,0.498039)">=</span> <span style="color:rgb(153,0,85)">10</span> <span style="color:rgb(166,127,89);background-color:rgba(255,255,255,0.498039)">*</span> <span style="color:rgb(0,119,170)">self</span><span style="color:rgb(153,153,153)">.</span>y
    <span style="color:rgb(153,153,153)">}</span>
<span style="color:rgb(153,153,153)">}</span><span style="width:3em;letter-spacing:-1px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(153,153,153)"><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span></span></code></pre></div><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">If the member is declared as <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">final</code> then it will be visible but not can be overrided by the derived classes. Just like it works with other access levels.</p><h3 style="margin:20px 0px 10px;padding:0px;font-size:18px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif">Value Types (structs, enums, etc.)</h3><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">Value types cannot have derived types. In this case the <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">protected</code> access level does not make sense and will not be allowed in their members.</p><h3 style="margin:20px 0px 10px;padding:0px;font-size:18px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif">Protocols</h3><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">Protocols do not declare access level for their members. So the <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">protected</code> access level is not applicable here.</p><h3 style="margin:20px 0px 10px;padding:0px;font-size:18px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif">Extensions</h3><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">Extensions will not be able do be protected nor their members.</p><h3 style="margin:20px 0px 10px;padding:0px;font-size:18px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif">Special Note</h3><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">The <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">protected</code> access level can only be applied to classes, structs and other types when nested inside other type. So the following code will not compile:</p><div style="color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px"><pre style="margin-top:0.5em;margin-bottom:0.5em;background-color:rgb(245,242,240);border:1px solid rgb(204,204,204);font-size:13px;line-height:1.5;overflow:auto;padding:1em 1em 1em 3.8em;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal"><code style="margin:0px;padding:0px;border:none;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-image:none;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal;line-height:1.5"><span style="color:rgb(112,128,144)">// ERROR: A first level class cannot be protected.</span>
protected <span style="color:rgb(0,119,170)">class</span> <span>MyProtectedClass</span> <span style="color:rgb(153,153,153)">{</span>
    <span style="color:rgb(112,128,144)">/// ...</span>
<span style="color:rgb(153,153,153)">}</span><span style="width:3em;letter-spacing:-1px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(153,153,153)"><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span></span></code></pre></div><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">But nested declarations will be allowed, so this code will compile:</p><div style="color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px"><pre style="margin-top:0.5em;margin-bottom:0.5em;background-color:rgb(245,242,240);border:1px solid rgb(204,204,204);font-size:13px;line-height:1.5;overflow:auto;padding:1em 1em 1em 3.8em;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal"><code style="margin:0px;padding:0px;border:none;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-image:none;font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace;word-spacing:normal;word-wrap:normal;line-height:1.5"><span style="color:rgb(112,128,144)">// We can declare a protected class (or struct, enum, etc.) if</span>
<span style="color:rgb(112,128,144)">// and only if they are nested inside other type.</span>
<span style="color:rgb(0,119,170)">public</span> <span style="color:rgb(0,119,170)">class</span> <span>MyPublicClass</span> <span style="color:rgb(153,153,153)">{</span>
    protected <span style="color:rgb(0,119,170)">class</span> <span>MyProtectedClass</span> <span style="color:rgb(153,153,153)">{</span>
        <span style="color:rgb(112,128,144)">/// ...</span>
    <span style="color:rgb(153,153,153)">}</span>
<span style="color:rgb(153,153,153)">}</span>

<span style="color:rgb(112,128,144)">// Since `MyProtectedClass` is nested and protected we</span>
<span style="color:rgb(112,128,144)">// can have access to it here.</span>
<span style="color:rgb(0,119,170)">public</span> <span style="color:rgb(0,119,170)">class</span> <span>MyDerivedClass</span><span style="color:rgb(153,153,153)">:</span> <span style="color:rgb(102,153,0)">MyPublicClass</span> <span style="color:rgb(153,153,153)">{</span>
    <span style="color:rgb(0,119,170)">public</span> <span style="color:rgb(0,119,170)">func</span> <span style="color:rgb(221,74,104)">doSomething</span><span style="color:rgb(153,153,153)">(</span><span style="color:rgb(153,153,153)">)</span> <span style="color:rgb(153,153,153)">{</span>
        <span style="color:rgb(0,119,170)">let</span> c <span style="color:rgb(166,127,89);background-color:rgba(255,255,255,0.498039)">=</span> <span style="color:rgb(221,74,104)">MyProtectedClass</span><span style="color:rgb(153,153,153)">(</span><span style="color:rgb(153,153,153)">)</span>
        
        <span style="color:rgb(112,128,144)">/// ...</span>
    <span style="color:rgb(153,153,153)">}</span>
<span style="color:rgb(153,153,153)">}</span><span style="width:3em;letter-spacing:-1px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(153,153,153)"><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span><span style="display:block"></span></span></code></pre></div><h2 style="margin:20px 0px 10px;padding:0px;font-size:24px;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204,204,204);color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif">Impact on existing code</h2><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">The imported Cocoa frameworks may have annotations on the &quot;Objective-C side&quot; that will inform if one member is <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">protected</code>. If this will be case (for the <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">layoutSubviews</code> method, as example), only code accessing these now protected members outside the owner class and its derived classes will have to change (currently this can be considered an error, anyway).</p><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">Any other code will be not impacted by this feature as it is new to the language.</p><h2 style="margin:20px 0px 10px;padding:0px;font-size:24px;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204,204,204);color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif">Alternatives considered</h2><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">Do not add the <code style="margin:0px 2px;padding:0px 5px;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">protected</code> access level to the current model.</p><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px"><br></p><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">---</p><p style="margin:15px 0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px"><br></p></div><div><br></div><div><br></div></div>
</blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>