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

Felipe Cypriano felipe at cypriano.me
Wed Dec 23 13:12:33 CST 2015


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".

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.

---

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.


More information about the swift-evolution mailing list