[swift-evolution] Analysis of existing scopes

Matthew Johnson matthew at anandabits.com
Thu Feb 23 07:59:05 CST 2017


> On Feb 23, 2017, at 4:47 AM, Joanna Carter via swift-evolution <swift-evolution at swift.org> wrote:
> 
> And now I find, what I consider to be, a very peculiar anomaly in method visibility.
> 
> private class PrivateBaseClass
> {
>  internal func internalFunc() { }
> 
>  public func publicFunc() { }
> 
>  open func openFunc() { }
> }
> 
> private class PrivateDerivedClass : PrivateBaseClass
> {
>  fileprivate override func internalFunc() { }
> 
>  fileprivate override func publicFunc() { }
> 
>  fileprivate override func openFunc() { }
> }
> 
> internal class InternalDerivedFromPrivateClass : PrivateBaseClass { } // error : Class cannot be declared internal because its superclass is fileprivate
> 
> 
> 
> Can somebody please explain why I can declare internal, public and open members on a private class, and even override them in a derived private class, all without even a compiler warning, when, as soon as I try to extend the visibility of a private class, I am told that I cannot so do.
> 
> I mean, it is blatantly obvious why you would not want higher visibility members in a limited visibility class, so why doesn't the compiler simply state that all members cannot have a visibility higher than the declaring class?

The rationale for this is that it allows you to explore the eventual design of a type without exposing it.  When you are confident in the design all you need to does modify the access modifier of the type itself.  Without this capability people might resort to comments indicating the intended future visibility and then have to remember to go back and convert all of those comments into the new access modifier when you’re ready to publish the type.

The important thing to keep in mind is that software isn’t a fixed, static thing.  It evolves over time.  This is a tool to help grow software over time.

> 
> --
> Joanna Carter
> Carter Consulting
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list