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

Javier Soto javier.api at gmail.com
Mon Dec 7 13:24:03 CST 2015


Filling up the rest of the details for the proposal:

*Motivation*
"It is not uncommon to have a need for a reference type without a need for
inheritance.  Superclasses should be intentionally designed to be
subclasses and the author required to opt-in to subclassing and member
overrides where that is required by the design."

*Proposed solution*
A keyword would be needed for both methods and classes. There are 2 options
(this is not a final proposal since I don't have great answers for all the
questions. Hoping other folks will chip-in :) )
- The keyword to specify that a class is "inheritable" is the same as the
keyword to specify that a method is "overridable": this has the benefit of
adding fewer keywords to the language, but it conflates 2 ideas that are
not exactly the same.
- Different keywords

As for what those keywords could be: Is there precedent in other languages
for final-by-default? The only example I know of kind-of this is C++, where
methods have to be specified "virtual" (even though that doesn't have quite
the same semantics).
I dislike the term "virtual" because it conveys implementation details,
rather than developer intention. Ideally the keywords would communicate "I
meant for this class to be subclassable, and these are the methods that you
can / must override ", and users of Swift shouldn't have to know what a
v-table is.

I don't have great ideas for keywords. All the ones I can think of are:
"nonfinal" "inheritable" "overridable", which are not great to say the
least.

*Other aspects*
When methods are overriden, design often requires that the super
implementation be called, sometimes even at a specific time (before / after
the new implementation). Should this change also provide support for this
sort of pattern, or would that be beyond the scope? (Note that this was
possible in Obj-C with *NS_REQUIRES_SUPER*)

*Impact on existing code*
This change would break any classes that subclass a class that hasn't been
marked as "non-final", and all methods that override the implementation of
a method also not marked as "non-final".
The compiler could warn about this in the error however, with a message
along the lines of "*this class/method inherits/overrides a class/method
that's not marked with the "nonfinal" keyword*".

On Mon, Dec 7, 2015 at 11:12 AM Javier Soto <javier.api at gmail.com> wrote:

> This was brought up in a different thread about private by default.
> Creating a new thread for this. Quoting Mathew from the other thread with a
> short summary about the motivation behind this:
>
> "It is not uncommon to have a need for a reference type without a need for
> inheritance.  Superclasses should be intentionally designed to be
> subclasses and the author required to opt-in to subclassing and member
> overrides where that is required by the design."
> --
> Javier Soto

-- 
Javier Soto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151207/c3900ebe/attachment.html>


More information about the swift-evolution mailing list