[swift-evolution] [Pitch] Requiring proactive overrides for default protocol implementations.

Erica Sadun erica at ericasadun.com
Thu Apr 28 21:40:06 CDT 2016


> On Apr 28, 2016, at 8:25 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
> 
> On Apr 28, 2016, at 8:11 PM, Xiaodi Wu <xiaodi.wu at gmail.com <mailto:xiaodi.wu at gmail.com>> wrote:
>> 
>> Sorry, stripped out a little too much, I guess. Let me expand a little:
>> 
>> In this example, `PortedTransform` has, by virtue of how it works, an upper bound and lower bound for valid input (among other interesting methods and properties). Exceed those bounds for your input and `PortedTransform` regurgitates garbage but does not throw any kind of error. Obviously, linear transform does not care about such silly things because it can transform essentially any FP input value, while the log transform simply traps when it encounters a negative value (which, as a precondition, it should never encounter).
>> 
>> BLAS is an accelerated linear algebra library; Apple has implemented a very nicely optimized one as part of its Accelerate framework. I use BLAS to sum, for example, two arrays of floating point values--it's very, very highly optimized. In that situation, there's no trapping when a single value is out of bounds (I get NaNs instead), and thus I must determine bounds in order to anticipate when the output will be garbage or NaN. (There are, as part of the Accelerate framework, accelerated functions to clamp entire arrays to given bounds with maximal efficiency).
>> 
>> For accelerated scaling and unscaling, then, it is essentially always necessary to compute upper and lower bounds even when that's unnecessary for non-accelerated scaling and unscaling, which operates on one value at a time. For that reason, `AcceleratedTransform` requires methods that compute upper and lower bounds, and provides a default implementation of accelerated clamping that calls those bound-computing methods and then uses the results as parameters when calling functions in Accelerate.framework. Methods for the computation of bounds already exist in `PortedTransform` but not in my own transforms. With your proposal, how would I retroactively model this requirement without touching code for `PortedTransform` and without compiling this one class into its own library? I'd like to be able to take advantage of the maximum possible compiler optimization, and optimizing across module boundaries is (as far as I understand) a little dicier. (Moreover, for MyLinTransform, I override the clamping method to return the input without calling out to any framework functions because I know a priori that the bounds are -infinity and infinity. I think that override will still be possible under your proposal, though.)
> 
> Without actually trying to understand the details of your math stuff:
> 
> * If you add a required member in a declaration or extension that declares conformance, it is 'required'. 
> * If it is already defaulted, it is `override required`. 
> * If it is already defaulted but not required, it is `override`
> * If someone else implements the stuff, you still have to pull it in somehow, but if you do so by conforming to another protocol with an extension, it's not your business, so you don't use any keywords.
> 
> You use keywords only for stuff that you specifically write, that clarifies the context in which you are writing it. If you do not own a protocol, an extension, or an implementation, you do not change or markup the protocol, extension, or implementation. You're just offering the compiler hints that your otherwise questionable decisions are fully intentional: when overriding an existing implementation and when conforming by supplying a required member.
> 
> -- E, who still probably missed your point and again apologizes

* If this is not Swift code it is not affected.
* If it is Swift code, either use #if swift(>= blah) workarounds or propose that SwiftPM support earlier Swift compilation rules.
* If this is adopted and the code is in Swift 3, it would already have compliances and you do not need to add anything

Under what scenario could you possibly use 3rd party Swift 3 code (assuming adoption) that would require annotation/changing?

--  Erica
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160428/49306490/attachment.html>


More information about the swift-evolution mailing list