Final by default for classes and methods

You're probably right. It's very likely that you have worked on more C++
codebases than I have, and I haven't been working on code for
high-performance computing, so it's possible that I'm suffering from a
small sample size. But if you're working on a consumer app, I do think that
it's logical vtable dispatch is what you want most of the time. So in my
experience, functions need to be virtual more often than not, and the C++
code I've seen would be shorter if you had to explicitly mark methods as
nonvirtual rather than virtual.

When I said, "programmers want virtual functions 99% of the time," I was
mostly thinking of the legion of programmers who grew up learning languages
where virtual methods are the only kinds of methods. Objective-C,
JavaScript, Ruby, Python, Java, etc. I've worked with a few younger
programmers who are thrown to the C++ sharks by management, and once they
learn the difference between virtual and non-virtual methods, they tend to
mark all their methods virtual as a defensive measure.

You make a very good point about Swift being the second major language to
take value semantics correctly. My original point though, was once most iOS
developers move to Swift, I think it's possible that they'll just stick to
what they're comfortable with, using classes exclusively and writing
Massive View Controllers, because it's what they know, it's easy to do, and
it doesn't require learning sometimes conceptually difficult new concepts.
So my question is whether we want to make that more difficult for them. It
seems like there are benefits and disadvantages to both. I'm just trying to
raise the possibility that this may be the dominant programming paradigm in
Swift for some time, as unfortunate as that may be.

···

On Mon, Dec 21, 2015 at 10:04 AM, Dave Abrahams <dabrahams@apple.com> wrote:

On Dec 20, 2015, at 3:51 PM, Michael Buckley via swift-evolution < > swift-evolution@swift.org> wrote:

+0. This seems reasonable, and a lot of the arguments are compelling. The
argument put forth about library design especially so. But coming from C++,
where I have to prefix nearly every method in my classes with virtual, I'm
worried that we could end up with the same problem in Swift.

We don't know what the dominant paradigm in swift will be ten years from
now. Inheritance has a raft of problems, but there's no guarantee that the
alternatives will be better in the long run. I suspect they will be, but I
also suspect we will find new and exciting problems in large codebases
using more functional patterns.

While there's a lot of excitement in the Swift community right now about
final, value types, and other language features, but I fear that when the
rest of the world jumps on the Swift bandwagon, most are just going to use
classes exclusively over structs and continue their OOP practices, simply
because it's what they're used to.

Making final the default may be a great way to discourage them. But it may
also get us right back to where we are in C++ today, where programmers want
virtual functions 99% of the time, but have to specify each function as
virtual.

In my considerable experience with C++, that is not at all where we are
today. Increasingly, C++ is becoming seen as a language for
high-performance computing, and people working in that area learn that they
don't want to pay for virtual dispatch when they don't have to. It is true
that for some of them, reflexive use of OOP is hard to shake, but they do
learn eventually. Note also that Swift is really the second major language
to take value semantics seriously. The first was C++.

On Sun, Dec 20, 2015 at 2:53 PM, Charles Srstka via swift-evolution < > swift-evolution@swift.org> wrote:

I agree with this. -1 to the proposal.

Charles

On Dec 17, 2015, at 8:00 PM, Rod Brown via swift-evolution < >> swift-evolution@swift.org> wrote:

To play devils advocate, take for example UINavigationController in UIKit
on iOS.

I’ve seen multiple times in multiple projects legitimate reasons for
subclassing it, despite the fact that UIKit documentation says we “should
not need to subclass it”. So if we relied on Apple to “declare”, they most
probably wouldn’t, and these use cases (and some really impressive apps)
would become impossible.

While I agree with all points made about “If it’s not declared
subclassable, they didn’t design it that way”, I think that ties everyone’s
hands too much. There is a balance between safety and functionality that
must be worked out. I think this errs way too far on the side of safety.

Rod

On 18 Dec 2015, at 12:51 PM, Javier Soto <javier.api@gmail.com> wrote:

What if one framework provider thinks “you won’t need to subclass this
ever”

If the framework author didn't design and implement that class with
subclassing in mind, chances are it's not necessarily safe to do so, or at
least not without knowledge of the implementation. That's why I think
deciding that a class can be subclassed is a decision that should be made
consciously, and not just "I forgot to make it final"
On Thu, Dec 17, 2015 at 5:41 PM Rod Brown <rodney.brown6@icloud.com> >> wrote:

My opinion is -1 on this proposal. Classes seem by design to
intrinsically support subclassing.

What if one framework provider thinks “you won’t need to subclass this
ever” but didn’t realise your use case for doing so, and didn’t add the
keyword? When multiple developers come at things from different angles, the
invariable situation ends with use cases each didn’t realise. Allowing
subclassing by default seems to mitigate this risk at least for the most
part.

I think this definitely comes under the banner of “this would be nice”
without realising the fact you’d be shooting yourself in the foot when
someone doesn’t add the keyword in other frameworks and you’re annoyed you
can’t add it.

On 18 Dec 2015, at 10:46 AM, Javier Soto via swift-evolution < >>> swift-evolution@swift.org> wrote:

Does it seem like there's enough interesest in this proposal? If so,
what would be the next steps? Should I go ahead and create a PR on the
evolution repo, describing the proposal version that Joe suggested, with
classes closed for inheritance by default outside of a module?

Thanks!

On Tue, Dec 8, 2015 at 7:40 AM Matthew Johnson via swift-evolution < >>> swift-evolution@swift.org> wrote:

I understand the rationale, I just disagree with it.

IMO adding a keyword to state your intention for inheritance is not a
significant obstacle to prototyping and is not artificial bookkeeping. I
really don't understand how this would conflict with "consequence-free"
rapid development. It is a good thing to require people to stop and think
before using inheritance. Often there is a more appropriate alternative.

The assumption that it is straightforward to fix problems within a
module if you later decide you made a mistake is true in some respects but
not in others. It is not uncommon for apps to be monolithic rather than
being well factored into separate modules, with many developers
contributing and the team changing over time. While this is not ideal it
is reality.

When you have the full source it is certainly *possible* to solve any
problem but it is often not straightforward at all. Here is an example of
a real-work scenario app developers might walk into:

1) A class is developed without subclassing in mind by one developer.
2) After the original developer is gone another developer adds some
subclasses without stopping to think about whether the original developer
designed for subclassing, thereby introducing subtle bugs into the app.
3) After the second developer is gone the bugs are discovered, but by
this time there are nontrivial dependencies on the subclasses.
4) A third developer who probably has little or no context for the
decisions made by previous developers is tasked with fixing the bugs.

This can be quite a knot to untangle, especially if there are problems
modifying the superclass to properly support the subclasses (maybe this
breaks the contract the superclass has with its original clients).

It may have been possible to avoid the whole mess if the second
developer was required to add 'inheritable' and 'overrideable' keywords or
similar. They are already required to revisit the source of it while
adding the keywords which may lead to consideration of whether the
implementation is sufficient to support inheritance in their currently
intended manner.

Implementation inheritance is a blunt tool that often leads to
unanticipated problems. IMO a modern language should steer developers away
from it and strive to reduce the cases where it is necessary or more
convenient. Making final the default would help to do this.

Supporting sealed classes and methods that can only be subclassed or
overridden within the same module is not in conflict with final by
default. Both are good ideas IMO and I would like to see both in Swift.

I hope the core team is willing to revisit this decision with community
input. If not I will let it go, although I doubt I will ever agree with
the current decision.

Matthew

Sent from my iPad

On Dec 7, 2015, at 10:30 PM, John McCall <rjmccall@apple.com> wrote:

>>> On Dec 7, 2015, at 7:18 PM, Matthew Johnson via swift-evolution < >>>> swift-evolution@swift.org> wrote:
>>> Defaults of public sealed/final classes and final methods on a
class by default are a tougher call. Either way you may have design issues
go unnoticed until someone needs to subclass to get the behavior they want.
So when you reach that point, should the system error on the side of rigid
safety or dangerous flexibility?
>>
>> This is a nice summary of the tradeoff. I strongly prefer safety
myself and I believe the preference for safety fits well with the overall
direction of Swift. If a library author discovers a design oversight and
later decides they should have allowed for additional flexibility it is
straightforward to allow for this without breaking existing client code.
>>
>> Many of the examples cited in argument against final by default have
to do with working around library or framework bugs. I understand the
motivation to preserve this flexibility bur don't believe bug workarounds
are a good way to make language design decisions. I also believe use of
subclasses and overrides in ways the library author may not have intended
to is a fragile technique that is likely to eventually cause as many
problems as it solves. I have been programming a long time and have never
run into a case where this technique was the only way or even the best way
to accomplish the task at hand.
>>
>> One additional motivation for making final the default that has not
been discussed yet is the drive towards making Swift a protocol oriented
language. IMO protocols should be the first tool considered when dynamic
polymorphism is necessary. Inheritance should be reserved for cases where
other approaches won't work (and we should seek to reduce the number of
problems where that is the case). Making final the default for classes and
methods would provide a subtle (or maybe not so subtle) hint in this
direction.
>>
>> I know the Swift team at Apple put a lot of thought into the
defaults in Swift. I agree with most of them. Enabling subclassing and
overriding by default is the one case where I think a significant mistake
was made.
>
> Our current intent is that public subclassing and overriding will be
locked down by default, but internal subclassing and overriding will not
be. I believe that this strikes the right balance, and moreover that it is
consistent with the general language approach to code evolution, which is
to promote “consequence-free” rapid development by:
>
> (1) avoiding artificial bookkeeping obstacles while you’re hacking
up the initial implementation of a module, but
>
> (2) not letting that initial implementation make implicit source and
binary compatibility promises to code outside of the module and
>
> (3) providing good language tools for incrementally building those
initial prototype interfaces into stronger internal abstractions.
>
> All the hard limitations in the defaults are tied to the module
boundary because we assume that it’s straightforward to fix any problems
within the module if/when you decided you made a mistake earlier.
>
> So, okay, a class is subclassable by default, and it wasn’t really
designed for that, and now there are subclasses in the module which are
causing problems. As long as nobody's changed the default (which they
could have done carelessly in either case, but are much less likely to do
if it’s only necessary to make an external subclass), all of those
subclasses will still be within the module, and you still have free rein to
correct that initial design mistake.
>
> John.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--

Javier Soto _______________________________________________

swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--

Javier Soto

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

-Dave

In C++, you also often see polymorphic type erasure containers built on top of types that themselves don't require dynamic dispatch, like `boost::any`, `std::function`, and the like. This is something Swift makes first-class with protocols and protocol types. You don't need virtual dispatch of implementations as much if you can introduce ad-hoc virtual dispatch of interfaces at any point.

-Joe

···

On Dec 21, 2015, at 10:04 AM, Dave Abrahams via swift-evolution <swift-evolution@swift.org> wrote:

On Dec 20, 2015, at 3:51 PM, Michael Buckley via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

+0. This seems reasonable, and a lot of the arguments are compelling. The argument put forth about library design especially so. But coming from C++, where I have to prefix nearly every method in my classes with virtual, I'm worried that we could end up with the same problem in Swift.

We don't know what the dominant paradigm in swift will be ten years from now. Inheritance has a raft of problems, but there's no guarantee that the alternatives will be better in the long run. I suspect they will be, but I also suspect we will find new and exciting problems in large codebases using more functional patterns.

While there's a lot of excitement in the Swift community right now about final, value types, and other language features, but I fear that when the rest of the world jumps on the Swift bandwagon, most are just going to use classes exclusively over structs and continue their OOP practices, simply because it's what they're used to.

Making final the default may be a great way to discourage them. But it may also get us right back to where we are in C++ today, where programmers want virtual functions 99% of the time, but have to specify each function as virtual.

In my considerable experience with C++, that is not at all where we are today. Increasingly, C++ is becoming seen as a language for high-performance computing, and people working in that area learn that they don't want to pay for virtual dispatch when they don't have to. It is true that for some of them, reflexive use of OOP is hard to shake, but they do learn eventually. Note also that Swift is really the second major language to take value semantics seriously. The first was C++.

I love those parts of Swift. Generics and value-type structs and high performance from static binding. But I also love UIKit and AppKit and the loosey-goosey but highly productive Objective-C style of dynamic binding and subclassability everywhere.

There’s a great balance here in Swift between ‘struct’ and ‘class’ and two very different styles of programming, and in my opinion, this proposal is trying to extend what ARE benefits of one half of the language in a way that is likely to wreck the other half of the language. Which is why I’m -1.

  - Greg

···

On Dec 21, 2015, at 10:04 AM, Dave Abrahams via swift-evolution <swift-evolution@swift.org> wrote:

In my considerable experience with C++, that is not at all where we are today. Increasingly, C++ is becoming seen as a language for high-performance computing, and people working in that area learn that they don't want to pay for virtual dispatch when they don't have to. It is true that for some of them, reflexive use of OOP is hard to shake, but they do learn eventually. Note also that Swift is really the second major language to take value semantics seriously. The first was C++.

Presumably a goal for Swift is that application developers will use it to build user-facing apps for Apple’s platforms. And presumably a goal for Apple is that developers help promote Apple’s platforms by shipping apps that take advantage of the new OS features when they ship. I fear that you and others dramatically underestimate the difficultly of doing that. I acknowledge your three points. But understand that we are professionals trying to serve our mutual customers. Temporary hacks in the service of shipping is the nature of the business.

I don’t know how to make the case more strongly than I already have. This thread makes me worry that the team does not understand what it’s like for third party developers trying to serve our mutual customers.

Sincerely,

Curt

···

On Dec 21, 2015, at 11:50 AM, Jordan Rose <jordan_rose@apple.com> wrote:

If you replace a method on someone else's class, you don't actually know what semantics they're relying on. Of course Apple code will have bugs in it. Trying to patch over these bugs in your own code is (1) obviously not an answer Apple would support, but also (2) fraught with peril, and (3) likely to break in the next OS release.

TLDR: It's already unsafe to do this with the existing set of Swift features. Yes, this makes things "worse", but it's not something we're interested in supporting anyway.

-----------------------------------------------------------------------------
Curt Clifton, PhD
Software Engineer
The Omni Group
www.curtclifton.net

Frankly, I think having `final` in the language at all is a mistake. While I agree that we should prefer composition to inheritance*, declaring things final is hubris. The only reasonable use case I've seen is for optimization, but that smacks of developers serving the compiler rather than the converse. Bringing an analog of NS_REQUIRES_SUPER to Swift would be most welcome; that's as far as I'd go down the path of dictating framework usage.

I really like the direction this discussion has taken ;-):
Is there any counter argument beside performance (which imho should always be seen under the aspect of premature optimization) that speaks against making NS_REQUIRES_SUPER the default behavior?

I personally don't like this but I can't put my finger on why. Obviously there are some things where you really don't need to call super (mostly abstract methods), but you just said "default", which implies that we could have an opt-out attribute.

I will say, however, that making NS_REQUIRES_SUPER the default for overridable methods is separable from deciding which methods are overridable by default. Making sure the base method is called isn't really the same as knowing the base method is all that's called.

Agree. There are at least four possibilities from most to least restrictive:

* not overridable
* overridable but requires a call to super in a specific location in the overriding method (i.e. the first or last line)
* overridable but requires a call to super somewhere in the overriding method
* overridable with no restrictions

···

On Dec 21, 2015, at 1:26 PM, Jordan Rose via swift-evolution <swift-evolution@swift.org> wrote:

On Dec 20, 2015, at 3:40 , Tino Heth via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Jordan

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I think this is potentially getting beyond Swift language development and into much wider platform concerns, and so I realize that I’m perhaps arguing at the wrong level and in the wrong place. Sorry about that. That having been said:

There is a really big design difference between a library like Swift’s stdlib or Foundation, which have fairly straightforward interfaces and simple program flow in and out. (In both of these cases you generally call in, and any calls out to application code are explicit and mostly short-lived closures.) AppKit or UIKit, on the other hand, are incredibly porous and have quite complicated program flow between the framework and the application code. The framework design is more like a skeleton upon which the application code hangs, and which in turns moves the kit objects about, rather than a self-contained system with a lot of invariants.

I can’t prove any causation, but I would certainly argue that the dynamic nature and possible overridability of even things that Apple doesn’t specifically intend to allow overriding is one of the primary reasons why AppKit has survived for 20+ years and spawned arguably the most successful application framework in history in UIKit. On the other hand, efficiency and safety have rarely been major issues.

TLDR: I don’t think using the design trade-offs of Array (which is, after all, a value type and can’t be subclassed anyway) inside stdlib, can be very usefully broadened to apply to reference types in application frameworks.

  - Greg

···

On Dec 21, 2015, at 11:50 AM, Jordan Rose via swift-evolution <swift-evolution@swift.org> wrote:

- There's a difference between "we're not going to optimize" and "we're not going to optimize now". Objective-C's "everything uses objc_msgSend" model is essentially unoptimizable. It's not that the developer can't work around that when performance is necessary; it's that the resulting code doesn't feel like Objective-C. Swift can do better, and even with its current semantics it does do better, for free. (And optimizations in frameworks are incredibly important. Where do you think your app spends most of its CPU time? I would guess for many many non-game apps, it's in framework code.)

- A major goal of Swift is safety. If you are writing a safe type built on unsafe constructs (like, say, Array), it is imperative that you have some control over your class invariants to guarantee safety. At the same time, your clients shouldn't have to know that you're built on unsafe constructs.

That last one is really the most important one. If you replace a method on someone else's class, you don't actually know what semantics they're relying on. Of course Apple code will have bugs in it. Trying to patch over these bugs in your own code is (1) obviously not an answer Apple would support, but also (2) fraught with peril, and (3) likely to break in the next OS release.

TLDR: It's already unsafe to do this with the existing set of Swift features. Yes, this makes things "worse", but it's not something we're interested in supporting anyway.

NS_REQUIRES_SUPER as the default is a bad idea. There's a big difference
between methods where you SHOULD call super and methods where you MUST
call super, and NS_REQUIRES_SUPER is meant for the latter case. The
problem with using it where you simply SHOULD call super is there's
occasionally a good reason to explicitly not call super, but there's no
way to suppress the warning in that case (and no obvious syntax to
propose for that). In addition, the cases where you MUST call super are
really extremely rare.

-Kevin Ballard

···

On Mon, Dec 21, 2015, at 11:26 AM, Jordan Rose via swift-evolution wrote:

On Dec 20, 2015, at 3:40 , Tino Heth via swift-evolution <swift- >> evolution@swift.org> wrote:

Frankly, I think having `final` in the language at all is a mistake.
While I agree that we should prefer composition to inheritance*,
declaring things final is hubris. The only reasonable use case I've
seen is for optimization, but that smacks of developers serving the
compiler rather than the converse. Bringing an analog of
NS_REQUIRES_SUPER to Swift would be most welcome; that's as far as
I'd go down the path of dictating framework usage.

I really like the direction this discussion has taken ;-): Is there
any counter argument beside performance (which imho should always be
seen under the aspect of premature optimization) that speaks against
making NS_REQUIRES_SUPER the default behavior?

I personally don't like this but I can't put my finger on why.
Obviously there are some things where you really don't need to call
super (mostly abstract methods), but you just said "default", which
implies that we could have an opt-out attribute.

I will say, however, that making NS_REQUIRES_SUPER the default for
overridable methods is separable from deciding which methods are
overridable by default. Making sure the base method is called isn't
really the same as knowing the base method is *all* that's called.

Obviously there are some things where you really don't need to call super (mostly abstract methods), but you just said "default", which implies that we could have an opt-out attribute.

I will say, however, that making NS_REQUIRES_SUPER the default for overridable methods is separable from deciding which methods are overridable by default. Making sure the base method is called isn't really the same as knowing the base method is all that's called.

Well, there are the two extreme positions:
final is as restrictive as possible and the current default, which allows to completely eliminate the inherited code.
NS_REQUIRES_SUPER (the name is imho the by far worst thing of this feature… inheritable sounds really better) is a compromise:
You can make sure that a method does what you want it to do without patronizing the user — and of course theres still room (and need) for final and nonfinal.

Tino

- 'private' and 'internal' methods are not exposed outside of a library, so you can't call them, much less override them. Similar for 'private' and 'internal' classes: you cannot subclass them.

really important point — and imho one of the best arguments not to make final the default:
It IS already the default (practically), so all points regarding own frameworks used by other parties become much less relevant.

In my considerable experience with C++, that is not at all where we are today. Increasingly, C++ is becoming seen as a language for high-performance computing, and people working in that area learn that they don't want to pay for virtual dispatch when they don't have to. It is true that for some of them, reflexive use of OOP is hard to shake, but they do learn eventually.

Is Swift's goal to be a perfect language for high-performance computing? I thought it was to be a language that scales from scripting to systems programming, with a nice sweet spot around application development.

For application development, speed is certainly good, but it is not the priority. We want Swift to go fast wherever we can get those speed gains without much cost, of course, but I worry that the cost of this proposal is too high. Just imagine going through UIKit and marking every class inheritable *by hand*—no cheating with a script—and you'll have some idea of the additional burden you'll be imposing on developers as they write their code. The proposals that every single method should be explicitly marked as overridable are even worse; frankly, I don't think I'd want to use Swift if you forced me to put a `virtual` keyword on every declaration.

I worry that the team's use of Swift to build the standard library, and their close association with teams building OS frameworks, is biasing the language a little bit. I think that, in all likelihood, most Swift code is in individual applications, and most libraries are not published outside of a single team. If I'm right, then most Swift code will probably be quite tolerant of small but technically "breaking" ABI changes, such as making a class `final`, or (as mentioned in another thread) making a closure `@noescape`.

That won't be true of published library code, of course. But published library code is a small minority of the Swift code people will write, and it already will require greater scrutiny and more careful design.

There is already a good opportunity to reflect on whether or not an API should be `final`. It's when you put the `public` keyword on it. I think programmers will have a better, easier time writing their code if, in this case, we put a little bit of trust in them, rather than erecting yet another hoop they must jump through.

Perhaps we could even provide a "strict interfaces" mode that published frameworks can turn on, which would require you to declare the heritability of every class and member. But even that may not be a good idea, because I also suspect that, in the field, most published libraries probably have to be extended in ways the library's author did not expect or anticipate.

This means doing some dangerous overriding, yes. But a UI that breaks after an iOS upgrade is not nearly as dangerous to my business as a three-month delay while I reimplement half of UIKit because someone in Cupertino thought they knew what I need better than I do and turned off—or even worse, *left turned off without a single thought*—subclassing of UIBarButtonItem.

The bottom line is this: Your users like Swift's strictures when they're helpful. *This stricture is not helpful.* Library users don't accidentally subclass things, and with the `override` keyword in Swift, they don't accidentally override them either. And where it truly is important, for safety or for speed, to prevent subclassing, we already have `final`. Making it the default is less safety than suffering.

···

--
Brent Royal-Gordon
Architechies

The benefits of it far out weight the fears of having it.

so what is the practical problem that's solved by final that convinced you?

I like to make those kind of questions to make people think about it
with an open mind. Currently the way I'm seeing it, the arguments
against it are mostly based on fear of change. It feeling that it could
be applied to other things in Swift like strong types "I hate that can't
just call this method on this AnyObject instance"; or access control "I
can't just perform selector on a private method anymore".

Mathew's summary has a list of 3 benefits of final by default that are
very compelling to me. I'm a strong believer that code should show its
author intention clearly, and not by just adding comments.

I also would change the list of downsides and put "annoyance" on top — especially for those who don't care for theoretical improvement when they have to pay the price in form of more effort:
"If I don't want to subclass something, I just don't do it — why do I have to change the properties of the superclass?"

Also:
- structs are always final, inheritance is one of the major aspects of class. In many cases, the decision for class is made because of the ability to subclass.
- you can't subclass across module borders with the default visibility
In summary, final already is very common, and I don't see the need to push this further just because "inheritance" became old-fashioned lately.

Best regards,
Tino

Good point about structs being final. But on the other hand having
classes as final by default will make the difference between structs and
classes even simpler, the only difference by default would be value
semantics but classes could opt-in for openness.

About sealed vs final. I think this is a do or do not, sealed would just
complicate things by having different behavior depending on where the
code is. I'd go with final by default or not go at all.

···

On Wed, Dec 23, 2015, at 09:25, Tino Heth wrote:

---

One final thing, I came across this article about [Designed
Inheritance](http://martinfowler.com/bliki/DesignedInheritance.html\) and
at the end the author mentions he would prefer a way to discourage
inheritance instead of prohibiting it. I'm intrigued by this idea and I
think I like it.

Sounds like you are going to continue using Objective-C until you can
get God on your team.

···

On Wed, Dec 23, 2015, at 11:23, Charles Srstka wrote:

On Dec 23, 2015, at 1:12 PM, Felipe Cypriano via swift-evolution <swift- >> evolution@swift.org> wrote:

On Wed, Dec 23, 2015, at 09:25, Tino Heth wrote:

The benefits of it far out weight the fears of having it.

so what is the practical problem that's solved by final that
convinced you?

I like to make those kind of questions to make people think about it
with an open mind. Currently the way I'm seeing it, the arguments
against it are mostly based on fear of change. It feeling that it
could be applied to other things in Swift like strong types "I hate
that can't just call this method on this AnyObject instance"; or
access control "I can't just perform selector on a private method
anymore”.

Or “I’ve had to work with other people’s C++ code before, and I know
what a PITA it is when there’s an issue you could easily solve by
subclassing and overriding a few methods, but the library author,
lacking ESP and knowledge of the future, didn’t anticipate that use
case.” Surely I can’t be the only one that’s happened to.

But don’t get me wrong, this proposal can work all right just as long
as we get rid of all human developers from all library and framework
projects, and hire God to do them instead. I wonder how much he
charges?

Charles

I changed my mind about sealed being a bad thing, this email from [John
McCall]
(https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001032.html\)
has very good points.

···

On Wed, Dec 23, 2015, at 11:12, Felipe Cypriano wrote:

About sealed vs final. I think this is a do or do not, sealed would
just complicate things by having different behavior depending on where
the code is. I'd go with final by default or not go at all.

---

Also I just noticed that this thread title mentions methods as final by
default, but I can't find an example/proposal of how it would work. My
question is is anyone suggesting that methods must be marked as
"overridable" even if the class is inheritable? E.g:

``` open class Money { func vanish() { } } ```

Is `vanish` final?

Fix-its solve this with a single click.

Stephen

···

On Dec 23, 2015, at 12:25 PM, Tino Heth via swift-evolution <swift-evolution@swift.org> wrote:

I also would change the list of downsides and put "annoyance" on top — especially for those who don't care for theoretical improvement when they have to pay the price in form of more effort:
"If I don't want to subclass something, I just don't do it — why do I have to change the properties of the superclass?"

I don't agree with this framing. IMO it encourages alternative designs emphasizing protocols and composition. This is a very good thing IMHO. I like to think of inheritance is a tool is last resort.

It does also do this...

BTW, I am planning a future proposal regarding automatic forwarding which if accepted would make the use of protocols and composition more convenient.

And this is a worthy goal.

But neither of these things has any bearing on the fact that sometimes, the library author just flat-out messes it up. And when they do, your choices are often to override something that's not meant to be overridden, or to reimplement the functionality yourself. All the protocols and composition in the world can't fix that.

(I do wonder, though, if we can have the fixability we need without the danger. Imagine if Swift included some facility to override a broken member even in a sealed class, but only for a particular version. So you could override a broken method on iOS 9.1, but the patch wouldn't be applied on 9.2 unless you tested it there and annotated your code to indicate the patch was also needed in that version.)

···

--
Brent Royal-Gordon
Architechies

I see the analogy, but IMO the issues involved in access control vs inheritance are significantly different. I agree with the default of internal for access control. However, I don’t want to get this thread sidetracked on the reasons why it is different and why I agree with that default.

Matthew

···

On Dec 23, 2015, at 12:59 PM, Stephen Celis <stephen.celis@gmail.com> wrote:

On Dec 23, 2015, at 1:55 PM, Matthew Johnson via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

By "pay a price" you mean diminished performance, right? That would depend on the ABI (which hasn't been discussed much yet, is there some preliminary docs about it?).

I don't think there is a price in performance to pay for sealed. You simply call a static function in the library, and that static function does the dynamic dispatch only if the library contains some overrides for that function. If there's no override it's simply purely a static call.

No, I don’t mean performance. I mean that the code is significantly less clear when final is not the default. It isn’t clear at all whether the author intended to allow subclasses or not when the default allows inheritance. The value in making this clear is significant, especially if you are a new developer walking into a large application.

While I agree with you, the same argument can be made for modules where `internal` code isn't marked `private`. Existing access control makes a case for `sealed` being the default, though I think class subclassing happens less frequently, and thus could be made `final` by default and utilize fix-its to make marking things inheritable simple enough.

Or “I’ve had to work with other people’s C++ code before, and I know what a PITA it is when there’s an issue you could easily solve by subclassing and overriding a few methods, but the library author, lacking ESP and knowledge of the future, didn’t anticipate that use case.” Surely I can’t be the only one that’s happened to.

But don’t get me wrong, this proposal can work all right just as long as we get rid of all human developers from all library and framework projects, and hire God to do them instead. I wonder how much he charges?

Charles

···

On Dec 23, 2015, at 1:12 PM, Felipe Cypriano via swift-evolution <swift-evolution@swift.org> wrote:

On Wed, Dec 23, 2015, at 09:25, Tino Heth wrote:

The benefits of it far out weight the fears of having it.

so what is the practical problem that's solved by final that convinced you?

I like to make those kind of questions to make people think about it
with an open mind. Currently the way I'm seeing it, the arguments
against it are mostly based on fear of change. It feeling that it could
be applied to other things in Swift like strong types "I hate that can't
just call this method on this AnyObject instance"; or access control "I
can't just perform selector on a private method anymore”.

The frameworks remaining useful simply because they happen to be written in a different language would be a pretty sad state of affairs, IMO.

Charles

···

On Dec 23, 2015, at 1:30 PM, Felipe Cypriano <felipe@cypriano.me> wrote:

Sounds like you are going to continue using Objective-C until you can get God on your team.

On Wed, Dec 23, 2015, at 11:23, Charles Srstka wrote:

On Dec 23, 2015, at 1:12 PM, Felipe Cypriano via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On Wed, Dec 23, 2015, at 09:25, Tino Heth wrote:

The benefits of it far out weight the fears of having it.

so what is the practical problem that's solved by final that convinced you?

I like to make those kind of questions to make people think about it
with an open mind. Currently the way I'm seeing it, the arguments
against it are mostly based on fear of change. It feeling that it could
be applied to other things in Swift like strong types "I hate that can't
just call this method on this AnyObject instance"; or access control "I
can't just perform selector on a private method anymore”.

Or “I’ve had to work with other people’s C++ code before, and I know what a PITA it is when there’s an issue you could easily solve by subclassing and overriding a few methods, but the library author, lacking ESP and knowledge of the future, didn’t anticipate that use case.” Surely I can’t be the only one that’s happened to.

But don’t get me wrong, this proposal can work all right just as long as we get rid of all human developers from all library and framework projects, and hire God to do them instead. I wonder how much he charges?

Charles

About sealed vs final. I think this is a do or do not, sealed would just
complicate things by having different behavior depending on where the
code is. I'd go with final by default or not go at all.

I changed my mind about sealed being a bad thing, this email from [John McCall](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001032.html\) has very good points.

---

Also I just noticed that this thread title mentions methods as final by default, but I can't find an example/proposal of how it would work. My question is is anyone suggesting that methods must be marked as "overridable" even if the class is inheritable? E.g:

The original post did indeed have that intention. However, pretty much the entire thread has been focused exclusively on classes, not methods. That has turned out to be a plenty big can of worms on its own.

I think it's best to keep this thread focused on classes. We can start a new thread for methods later if desired. But we should wait until the dust has settled on the classes conversation IMO.

···

Sent from my iPhone

On Dec 23, 2015, at 2:28 PM, Felipe Cypriano via swift-evolution <swift-evolution@swift.org> wrote:

On Wed, Dec 23, 2015, at 11:12, Felipe Cypriano wrote:

open class Money {
    func vanish() {
    }
}

Is `vanish` final?

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

"If I don't want to subclass something, I just don't do it — why do I have to change the properties of the superclass?"

Fix-its solve this with a single click.

Really? I guess I like this alternate reality — can the Xcode in your universe refactor Swift sources as well? ;-)