[swift-evolution] 'Public' class visibility specifiers

David Hart david at hartbit.com
Tue Feb 21 06:03:22 CST 2017

> On 21 Feb 2017, at 12:15, Joanna Carter via swift-evolution <swift-evolution at swift.org> wrote:
>> Le 21 févr. 2017 à 12:02, Brent Royal-Gordon <brent at architechies.com> a écrit :
>>> On Feb 20, 2017, at 8:47 AM, Joanna Carter via swift-evolution <swift-evolution at swift.org> wrote:
>>> When it comes to visibilities on classes, why, oh why do we have public vs open *as well as*
>>> the option of marking stuff as final?
>> We had an enormous, weeks-long, *hugely* contentious debate about whether to introduce `open`, and the end result was that SE-0117 was revised several times and finally approved. It describes the rationale concisely, but pretty well: <https://github.com/apple/swift-evolution/blob/master/proposals/0117-non-public-subclassable-by-default.md>
>> It doesn't *specifically* address why `final` was kept as well, but essentially, as the Motivation section describes, we wanted non-inheritable to be the default for public classes, so developers would have to specifically choose to open their classes to public subclassing. That meant that having `public` make the class subclassable would not fulfill our goals.
>> Look. It's not forbidden to re-litigate old design decisions, but if you're going to do it, you should at least study the original proposal and be prepared to either attack flaws in the Motivation section or show why the Proposed Design does not address the problem. Preferably, you should base your argument knowledge from later experience--things we did not know when we reviewed the proposal. You're not saying anything in this thread that wasn't said over and over again in a series of sprawling, stressful near-flamewars last summer.
>> Please don't tear open old wounds unless you at least have a new treatment to try.
> Firstly, let me apologise for any hurt caused, but it is quite difficult to carry on with you own projects *and* keep track of everything that is going on is Swift Evolution, especially with the current mailing list format, with interminable repeat quoting of whole threads in a single digest email.
> Now, if it was decided that non-inheritable would be the default for classes :
> 1. that's only true for inheritance outside of a module

I think that’s what he meant.

> 2. why leave 'final' in place (apart from Slava's reasons)?

It’s debatable, and if something is worth considering for removal its final. I’ve asked that exact question last month in the "final + lazy + fileprivate modifiers" thread got some interesting counter arguments:

Matthew Johnson:
My experience is that it is a very useful communication of intent to future readers of the code.
That aside, I think the criteria for a change are different now.  Your criteria were relevant during the big breaking change era of Swift 3.  I think the criteria for removing a feature at this point should be: is it causing problems that justify the breaking change required to remove it?

Charlie Monroe:
To me, it's useful a lot. The module doesn't necessarily be a 1KLOC framework - I've been recently refactoring a 90KLOC module and the final keyword was fairly useful since some subclasses used hacks by overriding some vars or methods. This allowed me to look at it from a different perspecitve, make some members final and create better API endpoints for customization.
Not to mention that it allows the compiler to access stored properties directly when they're final - if I recall correctly someone from the core team mentioning that.

Derrik Ho:
I find the final keyword useful when I want to communicate that this class should not be subclassed.
I think the behavior should remain the same since it is useful.

> 3. if inheritance of classes, which is a form of extension, is restricted outside of a module, why are extensions of a type allowed without restriction ?

Because extensions don’t have the same pitfalls as subclassing. The Motivation section of the proposal that introduced open that Brent linked has a very detailed explanations of the pitfalls of subclassing.

> Not forgetting that this discussion was started by me as part of a wider discussion of visibility and extensibility specifiers in general :-)
> --
> Joanna Carter
> Carter Consulting
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170221/4682b3a3/attachment-0001.html>

More information about the swift-evolution mailing list