[swift-evolution] Final by default for classes and methods

Michel Fortin michel.fortin at michelf.ca
Mon Dec 21 20:06:27 CST 2015

Le 7 déc. 2015 à 14:18, Slava Pestov via swift-evolution <swift-evolution at swift.org> a écrit :

> Another idea we've discussed internally is some form of 'sealed' classes. That is, classes that are publicly visible but only subclassable from within their module. This could be used to define closed inheritance hierarchies which give stronger guarantees to the optimizer.

Let's say I write an app. It has many classes but not too much subclassing and not too much overriding. I compile it with full module optimization and it runs fast and everything is great.

Then I move most of that code to a library or framework to make sharing code easier. Everything works, but then I notice the performance has dropped significantly. Why? "All you have to do is to slap `final` in front of every class everywhere" someone on swift-users says. Ah! Why? Then a debate follows about dynamic dispatch, inlining, and whether it's a good idea to make everything final in a library.

Sealed by default makes a lot of sense to me because code that works great in an app still works great when moved to a library.

Michel Fortin
michel.fortin at michelf.ca

More information about the swift-evolution mailing list