[swift-evolution] [swift-evolution-announce] [Accepted with Revision] SE-0177: Allow distinguishing between public access and public overridability

David Owens II david at owensd.io
Wed Jul 27 20:55:06 CDT 2016


Yes, it’s per file. It’s also added in the initial template that Xcode creates with your project. In addition, it’s recommended by many that talk about “how to unit test in Swift.” So, to someone that is not paying scrupulous attention, there is no mechanism to prevent against this today.

This also assumes that people write tests… which, well, we know is not the case at all.

I guess time will tell if this should be a warning/error or not depending on the usage of the various Swift linters that I’m sure will start to become prevalent. I know it’s something I’ll be enabling in my code.

It seems this design is acceptable and by-design.

-David


> On Jul 27, 2016, at 6:36 PM, Brent Royal-Gordon <brent at architechies.com> wrote:
> 
>> On Jul 27, 2016, at 4:41 PM, David Owens II via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> I brought this up in review, but there seems to be a serious testability problem here because of how special @testable is.
>> 
>> // This class is not subclassable outside of ModuleA.
>> public class NonSubclassableParentClass {
>>    // This method is not overridable outside of ModuleA.
>>    public func foo() {}
>> 
>>    // This method is not overridable outside of ModuleA because
>>    // its class restricts its access level.
>>    // It is not invalid to declare it as `open`.
>>    open func bar() {}
>> 
>>    // The behavior of `final` methods remains unchanged.
>>    public final func baz() {}
>> }
>> 
>> In a unit test, I *can* subclass `NonSubclassableParentClass`, the access level of `NonSubclassableParentClass` is irrelevant. There’s now no programatic way to ensure that I’m actually testing the contract that I had intended to provide to the consumers of my framework (that my class is subclassable). Is this really the intention?
> 
> I could be wrong, but isn't `@testable import` applied per-file? So if you keep code users should actually be able to write in one file with a non-`@testable` import, and mocks and other testing hacks in a different file with an `@testable` import, the first file should fail to compile if you use anything that's not normally permitted.
> 
> In a future version of Swift, we might consider refining this by requiring people to apply `@testable` directly to declarations which treat something closed as if it's open, but it seems like even the current feature set does not make testing impossible.
> 
> -- 
> Brent Royal-Gordon
> Architechies
> 



More information about the swift-evolution mailing list