[swift-evolution] [swift-evolution-announce] [Review #2] SE-0117: Default classes to be non-subclassable publicly

Peter Livesey pdlivesey at gmail.com
Tue Jul 19 17:26:43 CDT 2016


I'm new to this thread, but I've read every email so far. There are a few
things I find confusing with the proposal, so I'll try to sum up what I see
so far:

1. I don't understand what problem this solves?

It seems like for 3rd party libraries only, we are afraid that people will
subclass things when they shouldn't? Is this a real problem? I've honestly
never seen anyone incorrectly subclass something in a 3rd party library
when there was a good alternative. Maybe I have and I forget, but it
happens so rarely that I don't think it's a real problem to fix.

What are people frustrated by? Are 3rd party developers frustrated that
they can't specify this? But they can already - with final? Are consumers
frustrated that they're allowed to subclass when they shouldn't? I can't
imagine anyone wanting either of these things.

2. It seems that a consistent argument is that this 'forces developers to
think about it'.

I don't understand this at all. It's not a required keyword, so why does it
force me to do anything? I leave it out - there's no error, no reminder, no
forcing.

3. It's a crazy specific and complex rule:

"You can't subclass it only if it's public and only if it's in a different
module. Unless it's testable, then it's ok." Not only does this make many
assumptions about the roles of modules (not all modules are 3rd party
libraries), but it's a crazy rule for people to memorize. It definitely
violates the rule of least surprise in my opinion and I bet most people
running into this error will think: "What is this?".

4. Please can we keep swift simple

Swift has ~94 keywords so far (I know, that's not the best way to measure
complexity of a language), but does this problem really warrant it's own
keyword? Swift is beautiful because of how it balances power and
complexity. These types of rules add so much complexity for very little
benefit.

5. If you think subclassing is dangerous by default, turn it off by default.

Why are we special casing 3rd party libraries? Since when does a language
assume how it's being consumed? Again, this goes back to the assumption
that all modules are 3rd party libraries with well thought out APIs. I
don't think this is true at all and don't think it's the languages role to
force this model. If we truly believe that subclassing is dangerous unless
the class is designed for it, then lets do it everywhere, not just for
public classes. I don't think I'd vote for this, but I will admit it at
least has consistency and simplicity.


Summary

I think it's dangerous for a language to assume how code is written and
used. This proposal assumes that classes are written in a certain way, and
code is consumed in a certain way. I think Swift should stay flexible while
allowing for people to be expressive (e.g. the final keyword is AWESOME).
The pros here seem to suggest that we'll fix a bunch of bugs where
subclassing was done by mistake. The cons seem to be that it shackles
developers when people forget to add this keyword (which will happen a
lot...because there are 94 others to remember how to use correctly).


On Tue, Jul 19, 2016 at 2:17 PM Goffredo Marocchi via swift-evolution <
swift-evolution at swift.org> wrote:

>
>
> Sent from my iPhone
>
> On 19 Jul 2016, at 21:41, David Owens II <david at owensd.io> wrote:
>
>
> On Jul 19, 2016, at 11:37 AM, L. Mihalkovic via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>
>
> Regards
> (From mobile)
>
> On Jul 19, 2016, at 8:19 PM, Goffredo Marocchi via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>
> Sent from my iPhone
>
> <off-topic>
> Cocoa currently hides the boilerplate for all of these wonderful
> constructs behind amazingly effective runtime acrobatics. This fits
> perfectly into Objective-C, and it also works very well in Swift. But such
> features could be in better harmony with Swift's unique set of language
> constructs if their boilerplate was hidden behind amazingly effective
> **compile-time** acrobatics instead.
>
> Such compile-time acrobatics are hard to perform today, and it is possible
> that the ability to create such systems will forever remain an advanced
> skill, just like forging runtime magic requires advanced skills in
> Objective-C.
>
>
> ... rantish...
>
> I am still not convinced that even the best compiler can fully replace
> what a powerful runtime can provide no matter the acrobatics you put in in
> terms of compiler introduced utility code/constructs or the code analysis
> efforts you can put in at compile time
>
>
> That is a fact back by some interesting papers. By it is also true that
> one cannot always be used in place of the other.
>
>
> While it is true that the compiler cannot fully replace everything, it can
> help with many types of errors that are hard to catch at runtime.
>
> I’ll reiterate this again: this proposal does *not* prevent Swift from
> building language mechanism to allow for this type of behavior though. The
> fact is, Swift APIs are heavily value or value semantics so the lack of
> inheritance on classes is not going to be the primary reason you cannot
> monkey patch something.
>
> What this proposal does say is this: there is no “safe” way to achieve
> this type of behavior that the API authors either intentionally left out or
> left out because of an omission.
>
> If you are subclassing a class in Swift, under this proposal, you can be
> sure of one thing: the author explicitly made it so you’d be able to. We
> can argue if they did so with diligence, but that’s mostly immaterial to
> the discussion.
>
> The fact is, in order to allow monkey patching, Swift is going to need to
> answer many questions that it still has yet to answer, such as how
> reflection is really going to work. However, with these more restrictive
> defaults, it’s possible to extend the language to provide runtime monkey
> patching that can be more easily audited both in source code and at
> runtime. Instead of API authors just seeing crashes in their libraries and
> not realizing that someone swizzled a method, we would now be able to mark
> who did the swizzling and which functions were actually swizzled.
>
> An example of how this *could* look would be something like this:
>
> struct Person { /* This is defined in module A */
>     // a bunch of fields
>     var birthDate: Date { get set }
>
>     func calculateAge() -> Int { return 12; }
> }
>
> Obviously there is a bug in `calculateAge`. How could we fix it? Well, in
> your app framework (or some other target as needed) as we don’t have the
> ability to fix module A directly:
>
> extension Person {
>     @replaceMethodImplementation func calculateAge() -> Int { /* better
> implementation */ }
> }
>
> This would require support within the Swift language and tooling. For one,
> we’d need the non-optimized code to be distributed so that code within
> module A used this new implementation of `calculateAge`. However, this is
> the direction I’d much rather see Swift go. The ability to annotate code
> properly as monkey patches so we can audit them, track them, version them,
> and identify them clearly both at compile time and runtime.
>
> I’m all for Swift getting the right underlying model. After that, we can
> address a better way to provide the necessary means to monkey patch and
> change behavior, but first the fundamentals need to be nailed properly.
>
>
> Agreed, but we need to get ourselves some metrics and not to be afraid to
> considered annoying non true believers :). As more coders use Swift in
> their apps for some portions or the entirety of their codebase, are we
> seeing less bugs? When will it be proper to collect the data in a
> productive non biased way? I think it'll be very interesting as more stats
> are available other than LOC ones.
>
> We desire to have Swift run in all possible scenarios, complex video games
> included, and being a heavily value type based language with many built-in
> components using CoW semantics (sizeable in memory copies when some coders
> may forget it will happen). There is a reason why academia and a lot of
> coders back then we're pushing people towards reference types and pass by
> reference semantics and away from how C dealt with big structs and function
> pointers to achieve similar features.
>
>
> -David
>
> _______________________________________________
> 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/20160719/93f42f18/attachment.html>


More information about the swift-evolution mailing list