The default access level is internal by default, not public. While there are good reasons to not make members public by default, it is important that members are internal by default.<br><br>One important reason: progressive disclosure. Access modifiers make sense after you understand what encapsulation is all about. Encapsulation only really makes sense after you can actually write a type. If members are private by default, you cannot write a useful type without also learning access modifiers!<br><br>Put another way:<br><br>struct Point {<br> let x: Int<br> let y: Int<br>}<br><br>...is by itself a useful type. If x and y were private, then this type would do nothing. Users would have to learn about access modifiers before they can write a useful type.<br><div class="gmail_quote"><div dir="ltr">On Mon, Apr 3, 2017 at 18:13 Ted F.A. van Gaalen via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg">Hello, <div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">(this is related to: <span style="font-family:Avenir-Medium" class="gmail_msg"> Type-based ‘private’ access within a file)</span></div><div class="gmail_msg"><br class="gmail_msg"><div class="gmail_msg">Currently class and struct items (variables, functions..) are public by default</div><div class="gmail_msg">that is, always accessible outside the class’s scope</div><div class="gmail_msg">for example: (Swift 3.1) </div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">class</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg"> AsItIsNow</span></font></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font size="2" class="gmail_msg">{</font></span></div><p style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">private</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">var</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> a = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="gmail_msg">10</span></p><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">private</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">var</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> b = </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="gmail_msg">12</span></font></div><p style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><br class="m_6063390315337117777webkit-block-placeholder gmail_msg"></font></p><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">func</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> aPlusB() -> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="gmail_msg">Int</span></font></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font size="2" class="gmail_msg"> {</font></span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="gmail_msg">return</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">a</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> + </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="gmail_msg">b</span></font></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font size="2" class="gmail_msg"> }</font></span></div><p style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><br class="gmail_msg"></span></p><p style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">private</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">func</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> helperFunction()</span></p><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font size="2" class="gmail_msg"> {</font></span></div><p style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font face="Menlo" class="gmail_msg"> . . . </font></span></font></p><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font size="2" class="gmail_msg"> }</font></span></div><p style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-size:small" class="gmail_msg">}</span></p></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Having all items public by default has the following disadvantages:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"> • All members are exposed by default, wether intended or not. This</div><div class="gmail_msg"> could lead to class misusage unintended by the class’s creator. </div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"> • To prevent undesired access of functions vars etc. inside a class</div><div class="gmail_msg"> one has to prefix often too many items with the ‘private’ access modifier keyword.</div><div class="gmail_msg"><div class="gmail_msg"> Most of the functionality and items inside a class or struct are meant to </div><div class="gmail_msg"> be used internally and should not be revealed to the outside world. </div></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"> • It does not comply (contradicts) with lexical scope as in Swift functions etc. </div><div class="gmail_msg"> </div><div class="gmail_msg"> • It is frontally different to most other programming languages and</div><div class="gmail_msg"> therefore confusing for those coming from C#, C++, Java, Pascal etc. </div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I experience this as fundamentally wrong. </div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Imho it should be as in the next example, everything in a class</div><div class="gmail_msg">is private by default. To access/deploy items from outside they </div><div class="gmail_msg">need to be defined with the ‘public’ access modifier, </div><div class="gmail_msg">not the other way around, please. </div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">class</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)" class="gmail_msg"> AsItShouldBeImho</span></font></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font size="2" class="gmail_msg">{</font></span></div><div style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">var</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> a = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="gmail_msg">10 // private by default</span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">var</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> b = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="gmail_msg">12 // also</span></font></div><div style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><br class="m_6063390315337117777webkit-block-placeholder gmail_msg"></font></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> public </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">func</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> aPlusB() -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="gmail_msg">Int</span></font></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font size="2" class="gmail_msg"> {</font></span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="gmail_msg">return</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="gmail_msg">a</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> + </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="gmail_msg">b</span></font></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font size="2" class="gmail_msg"> }</font></span></div><div style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-size:small" class="gmail_msg"> </span></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"> func private</span><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg">Function() </span></font></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font size="2" class="gmail_msg"> {</font></span></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font size="2" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font face="Menlo" class="gmail_msg"> . . . </font></span></font></div><div style="margin:0px;line-height:normal;font-family:Menlo" class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font size="2" class="gmail_msg"> }</font></span></div><div style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-size:small" class="gmail_msg">}</span></div></div><div style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-size:small" class="gmail_msg"><br class="gmail_msg"></span></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">‘public’ could be an inferred attribute</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">for items defined with getters and setters.. </font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">or the compiler would demand public</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">for getters and setters..</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg"><br class="gmail_msg"></font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">The ‘protected’ keyword is needed. </font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">‘protected’ would mean that the items are</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">accessible in subclasses (class only) </font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg"><br class="gmail_msg"></font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">private items would also be inaccessible in</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">class extensions. </font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg"><br class="gmail_msg"></font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">It is as yet unclear to me why it has not been done</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font size="2" class="gmail_msg"><font face="Avenir-Book" class="gmail_msg">like so from the beginning of Swift</font><font face="Avenir-Book" class="gmail_msg">…</font></font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg"><br class="gmail_msg"></font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">I’ve been lost in the eternal discussions over private access modifiers,</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">the volume of it makes me think that (either way in this discussions) the whole</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">scope definition is wrong in Swift .</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">especially using a file as scope limiter. </font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">Imho there should be lexical scope only.</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">as in most other programming languages.</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">If done so correctly , one wouldn’t even need the ‘private’ keyword...</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg"><br class="gmail_msg"></font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">Kind Regards</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg">TedvG</font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg"><br class="gmail_msg"></font></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><a href="http://www.tedvg.com" class="gmail_msg" target="_blank"><font face="Avenir-Book" size="2" class="gmail_msg">www.tedvg.com</font></a></div><div style="margin:0px;line-height:normal;min-height:21px" class="gmail_msg"><font face="Avenir-Book" size="2" class="gmail_msg"><a href="http://www.ravelnotes.com" class="gmail_msg" target="_blank">www.ravelnotes.com</a></font></div><div style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-size:small" class="gmail_msg"><br class="gmail_msg"></span></div><div style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div></div><div style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-size:small" class="gmail_msg"><br class="gmail_msg"></span></div><div style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-size:small" class="gmail_msg"><br class="gmail_msg"></span></div><div style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-size:small" class="gmail_msg"><br class="gmail_msg"></span></div><div style="margin:0px;line-height:normal;font-family:Menlo;min-height:21px" class="gmail_msg"><span style="font-size:small" class="gmail_msg"><br class="gmail_msg"></span></div><div class="gmail_msg"><span style="font-variant-ligatures:no-common-ligatures" class="gmail_msg"><font size="2" class="gmail_msg"><br class="gmail_msg"></font></span></div><div class="gmail_msg"> </div><div class="gmail_msg"><br class="gmail_msg"></div></div></div>_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>