<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br><br>Sent from my iPad</div><div><br>On Jul 10, 2016, at 10:38 PM, Jordan Rose via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8">[Proposal: <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0117-non-public-subclassable-by-default.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0117-non-public-subclassable-by-default.md</a> ]<div class=""><br class=""></div><div class="">(This is my second response to this proposal. The previous message shared a use case where public-but-non-subclassable made things work out much better with required initializers. This one has a bit more ideology in it.)</div><div class=""><br class=""></div><div class="">As many people have said already, this proposal is quite beneficial to library designers attempting to reason about their code, not just now but in the future as well. The model laid out in the&nbsp;<a href="http://jrose-apple.github.io/swift-library-evolution/" class="">Library Evolution document</a>&nbsp;(often referred to as “resilience”) supports Swift libraries that want to preserve a stable binary and source interface.</div><div class=""><br class=""></div><div class="">In the Swift 2 model (and what’s currently described in that document), a public class must be final or non-final at the time it is published. It’s clearly not safe to <i class="">add</i>&nbsp;‘final' in a later version of the library, because a client might already have a subclass; it’s also not safe to <i class="">remove</i>&nbsp;‘final’ because existing clients may have been compiled assuming there are no subclasses.</div><div class=""><br class=""></div><div class="">(Of course, we can remove this optimization, and make ‘final’ a semantic contract only. I’m deliberately avoiding most discussion of performance, but in this parenthetical I’ll note that Swift makes it possible to write code that is <i class="">slower</i>&nbsp;than Objective-C. This is considered acceptable because the compiler can often optimize it for a particular call site. For those who want more information about the current implementation of some of Swift’s features, I suggest watching the “<a href="https://developer.apple.com/videos/play/wwdc2016/416/" class="">Understanding Swift Performance</a>” talk from this year’s WWDC.)</div><div class=""><br class=""></div><div class="">With this proposal, a public class can be non-publicly-subclassable or publicly-subclassable. Once a class is publicly-subclassable (“open”), you can’t go back, of course. But a class that’s not initially open could <i class="">become</i> open in a future release of the library. All existing clients would already be equipped to deal with this, because there might be subclasses <i class="">inside</i>&nbsp;the library. On the other hand, the class can <i class="">also</i>&nbsp;be marked ‘final’, if the library author later realizes there will never be any subclasses and that both client authors and the compiler should know this.</div><div class=""><br class=""></div><div class="">One point that’s not covered in this proposal is whether making a class ‘open’ applies retroactively, i.e. if MagicLib 1.2 is the first version that makes the Magician class ‘open’, can clients deploy back to MagicLib 1.0 and expect their subclasses to work? My inclination is to say no; if it’s possible for a non-open method to be overridden in the future, a library author has to write their library as if it will be overridden now, and there’s no point in making it non-open in the first place. That would make ‘open’ a “<a href="http://jrose-apple.github.io/swift-library-evolution/#publishing-versioned-api" class="">versioned attribute</a>” in the terminology of Library Evolution, whatever the syntax ends up being.</div><div class=""><br class=""></div><div class="">---</div><div class=""><br class=""></div><div class="">Okay, so why is this important?</div><div class=""><br class=""></div><div class="">It all comes down to reasoning about your program’s behavior. When you use a class, you’re relying on the documented behavior of that class. More concretely, the methods on the class have preconditions (“performSegue(withIdentifier:sender:) should not be called on a view controller that didn’t come from a storyboard”) and postconditions (“after calling loadViewIfNeeded(), the view controller’s view will be loaded”). When you call a method, you’re responsible for satisfying its preconditions so it can deliver on the postconditions.</div><div class=""><br class=""></div><div class="">I used UIViewController as an example, but it applies just as much to your own methods. When you call a method in your own module—maybe written by you, maybe by a coworker, maybe by an open source contributor—you’re expecting some particular behavior and output given the inputs and the current state of the program. That is, you just need to satisfy its preconditions so it can deliver on the postconditions. If it’s a method in your module, though, you might not have taken the trouble to formalize the preconditions and postconditions, since you can just go look at the implementation. Even if your expectations are violated, you’ll probably notice, because the conflict of understanding is within your own module.</div><div class=""><br class=""></div><div class="">Public overriding changes all this. While an overridable method may have particular preconditions and postconditions, it’s possible that the overrider will get that wrong, which means the library author can no longer reason about the behavior of their program. If they do a poor job documenting the preconditions and postconditions, the client and the library will almost certainly disagree about the expected behavior of a particular method, and the program won’t work correctly.</div><div class=""><br class=""></div><div class="">"Doesn’t a library author have to figure out the preconditions and postconditions for a method anyway when making it public?" Well, not to the same extent. It’s perfectly acceptable for a library author to document stronger preconditions and weaker postconditions than are strictly necessary. (Maybe 'performSegue(withIdentifier:sender:)’ has a mode that can work without storyboards, but UIKit isn’t promising that it will work.) When a library author lets people override their method, though, they're promising that the method will never be called with a weaker precondition than documented, and that nothing within their library will expect a stronger postcondition than documented.</div><div class=""><br class=""></div><div class=""><div class="">(By the way, the way to look at overriding a method is the inverse of calling a method:&nbsp;<i class="">you</i>&nbsp;need to deliver on the postconditions, and you can assume the&nbsp;<i class="">caller</i>&nbsp;has satisfied the preconditions. If your understanding of those preconditions and postconditions is wrong, your program won’t work correctly, just like when you’re calling a method.)</div></div><div class=""><br class=""></div><div class="">This all goes double when a library author wants to release a new version of their library with different behavior. In order to make sure existing callers don’t break, they have to make sure all of the library’s documented preconditions are no stronger and postconditions are no weaker for public API. In order to make sure existing subclassers don’t break, they have to make sure all of the library’s documented preconditions are no <i class="">weaker</i>&nbsp;and postconditions are no <i class="">stronger</i>&nbsp;for overridable API.</div><div class=""><br class=""></div><div class="">(For a very concrete example of this, say you’re calling a method with the type '(Int?) -&gt; Int’, and you’re passing nil. The new version of the library can’t decide to make the parameter non-optional or the return value optional, because that would break your code. Similarly, if you’re <i class="">overriding</i>&nbsp;a method with the type ‘(Int) -&gt; Int?’, and <i class="">returning</i>&nbsp;nil, the new version of the library can’t decide to make the parameter <i class="">optional</i> or the return value <i class="">non-</i>optional, because <i class="">that</i>&nbsp;would break your code.)</div><div class=""><br class=""></div><div class="">So, "non-publicly-subclassable" is a way to ease the burden on a library author. They should be thinking about preconditions and postconditions in their program <i class="">anyway,</i>&nbsp;but not having to worry about all the things a client might do for a method that <i class="">shouldn’t</i>&nbsp;be overridden&nbsp;means they can actually reason about the behavior—and thus the correctness—of their own program, both now and for future releases.</div><div class=""><br class=""></div><div class="">---</div><div class=""><br class=""></div><div class="">I agree with several people on this thread that non-publicly-subclassable-by-default is the same idea as internal-by-default: it means that you have to explicitly decide to support a capability before clients can start relying on it, and you are very unlikely to do so by accident. The default is “safe” in that a library author can change their mind without breaking existing clients.</div><div class=""><br class=""></div><div class="">I agree with John that even today, the entry points that <i class="">happen</i> to be public in the types that <i class="">happen</i>&nbsp;to be public classes are unlikely to be good entry points for fixing bugs in someone else's library. Disallowing overriding these particular entry points when a client already can't override internal methods, methods on structs, methods that use internal types, or top-level functions doesn’t really seem like a loss to me.</div><div class=""><br class=""></div><div class="">Library design is important. Controlling the public interface of a library allows for better reasoning about the behavior of code, better security (i.e. better protection of user data), and better maintainability. And whether something can be overridden is part of that interface.</div><div class=""><br class=""></div><div class="">Thanks again to Javier and John for putting this proposal together.</div><div class="">Jordan</div></div></blockquote><div><br></div><div><span style="background-color: rgba(255, 255, 255, 0);">Thanks for this really excellent, detailed analysis of the rationale for making sealed the default Jordan!&nbsp;</span></div><br><blockquote type="cite"><div><div class=""><br class=""></div><div class="">P.S. There’s also an argument to be made for public-but-not-conformable protocols, i.e. protocols that can be used in generics and as values outside of a module, but cannot be conformed to. This is important for many of the same reasons as it is for classes, and we’ve gotten a few requests for it. (While you can get a similar effect using an enum, that’s a little less natural for code reuse via protocol extensions.)</div></div></blockquote><div><br></div><div>FYI - I have been planning to propose exactly this feature for protocols once we get past the Swift 3 additive feature freeze.</div><br><blockquote type="cite"><div><div class=""><br class=""></div><div class="">P.P.S. For those who will argue against “better security”, you’re correct: this <i class="">doesn’t</i>&nbsp;prevent an attack, and&nbsp;I <i class="">don’t</i> have much expertise in this area. However, I <i class="">have</i>&nbsp;talked to developers distributing binary frameworks (despite our warnings that it isn’t supported) who have asked us for various features to keep it from being <i class="">easy.</i></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>