<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 25, 2017, at 4:23 PM, Jonathan Hull &lt;<a href="mailto:jhull@gbis.com" class="">jhull@gbis.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 25, 2017, at 1:43 PM, Matthew Johnson via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 25, 2017, at 3:27 PM, Kevin Nattinger via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class=""><div class=""><font class="">…</font></div></div></div></blockquote><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class="">Additionally, the design allows ‘final’ to take any one of those visibility levels as a parameter, to indicate that the type should be treated as ‘final’ at and above the specified scope. Thus ‘final(public)’ prevents subclassing outside the module, while ‘final(internal)’ prevents it outside the ‘private’ scope. For consistency, ‘final(private)’ is also permitted, although it means the same thing as ‘final’ by itself.</div></div></div></div></blockquote><br class=""></div><div class="">I feel final(public) could be confusing given how we currently have private(get/set). What about public(final)? That’s at least consistent with current access syntax.</div></div></div></blockquote><div class=""><br class=""></div><div class="">The syntax that most closely aligns with private(set) is internal(inherit). &nbsp;The parameter expresses a capability not a limitation and it is used with an access modifier that specifies the *maximum* access level that is allowed to have this capability. &nbsp;`final` is also misleading in this context because it implies to most people that there are *no* subclasses (inside or outside the module). &nbsp;</div><div class=""><br class=""></div><div class="">The only way to express `final` in Swift’s access control system would to have a `never` access modifier allowing you to say `never(inherit)`. &nbsp;I can’ think of any other uses for an access level like this. &nbsp;`final` is much more direct and is the term of art. &nbsp;I don’t see a reason to change the spelling of `final` and parameterizing it doesn’t make any sense - if something is `final` it is `final` in *all* scopes.</div></div></div></div></blockquote><div class=""><br class=""></div><div class="">hmm… ‘semifinal' for things that are closed outside the submodule?</div></div></div></div></blockquote><div><br class=""></div><div>The name that has usually been used for this is `closed`. &nbsp;But no word that expresses a *restriction* is going to fit well into Swift’s access control system. &nbsp;The approach of Swift’s access control system (which I believe to be the best approach) is to express an upper bound on a *capability*. &nbsp;The basic capability is visibility of the symbol. &nbsp;That model is extended by talking about specific things a user might *do* with that symbol and setting an upper bound on that specific use: `internal private(<b class="">set</b>)`. &nbsp;`internal` is the bound on visibility and `private` is the bound on the ability to `set` the property. &nbsp;</div><div><br class=""></div><div>`closed` is most naturally expressed in this system as `public internal(<b class="">inherit</b>)`. &nbsp;One interesting thing to observe is that `internal` is the default level of visibility. If we extend that default to capabilities as well as visibility the result is to give us exactly the behavior we want from a naked `public` annotation. &nbsp;We want to require an annotation for exposing anything (a symbol *or* a capability) outside the module. &nbsp;This means that `open` is really an alias for `public(inherit)` if we follow the principles underlying the model we have for access control.</div><div><br class=""></div><div>Following this line of reasoning to its natural conclusion we can observe that the current interaction of `var` properties with access control *do not* follow the principles we have set forth for Swift. &nbsp;`public` variables *automatically* have `public` getters violating the principle that `internal` is the default. &nbsp;We could fix this by requiring users to say `public(set)`. &nbsp;I imagine there would be a lot of screaming in the short term if we proposed this. &nbsp;But it would put Swift’s access control system on a more consistent and principled footing. &nbsp;</div><div><br class=""></div><div>I think there are three major reasons people have trouble with the current access control system. &nbsp;The `private` / `fileprivate` name situation is an obvious one. &nbsp;But I think perhaps more important is the fact that the system has idiosyncrasies such as introducing `open` rather than following the capability model of `set` (which would have resulted in `public(inherit)`). &nbsp;Another idiosyncrasy noted above is that `set` defaults to the level of the getter rather than `internal` (but bounded by visibility). &nbsp;Finally, the principle that underlies the access modifiers (modulo the idiosyncrasy of `open`) is that of a bounded scope. &nbsp;But this principle is implicit - each keyword is defined on an ad-hoc basis rather than making the underlying principle explicit and defining any shorthand we want in terms of that (as syntactic sugar for common cases and recommended defaults).</div><div><br class=""></div><div>This is why I strongly believe the best approach is to make the underlying principle of scopes and capabilities with an `internal` default explicit. &nbsp;I think this principle is easy to teach and easy to understand once it is made explicit. &nbsp;It is a wonderful (but currently rather hidden) aspect of Swift. &nbsp;If we can teach users to understand this principle and to learn the names of scopes and capabilities they will not find it difficult and complex. &nbsp;They will find it as easy to learn as a library function: we would have `scope(&lt;capbability name&gt;, &lt;scope name&gt;)`**. &nbsp;The capability would be defaulted to basic visibility. &nbsp;The scope would be defaulted to the current scope. &nbsp;</div><div><br class=""></div><div>Consider&nbsp;scope(inherit, public)`. &nbsp;It would read like “the scope of inheritance is public”. &nbsp;It tells the reader *exactly* what is happening. &nbsp;We could conceptualize shortened like `open` as something like `accessalias open = scope(inherit, public)`. &nbsp;Tools could even allow you to cmd-Click to the definition to see a definition like this. &nbsp;Users would learn something that is no more complicated than a single function call and any time they are confused by shorthand they could cmd-Click or look up documentation that explains it clearly in terms of this "function call”. &nbsp;Note: `accessalias` wouldn’t be a real thing, just a way to document and explain the shorthand keywords we have for “soft defaults” and common conveniences.</div><div><br class=""></div><div>This seems to me like a very simple and elegant yet extraordinarily powerful system. &nbsp;I think it’s much better than a small collection of ad-hoc definitions of very specific behaviors.</div><div><br class=""></div><div>**Anyone who has been reading my thoughts on this might notice that I am using `scope` rather than `scoped` in this post. &nbsp;As I was writing this I realized that when we think of access in terms of capability it reads better with `scope` than it does with `scoped` (i.e. “inheritance is scoped public” does not read as well as “the scope of inheritance is public".</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class="">Thanks,</div><div class="">Jon</div></div></div></blockquote></div><br class=""></body></html>