Reputation: 15706
Why does this compile?
internal class A {
public func f() {
}
}
I expected f's "public" modifier to be disallowed because its enclosing class is internal.
Upvotes: 6
Views: 632
Reputation: 80931
One motivation for allowing this is mentioned in SE-0025: Scoped Access Level (emphasis mine):
The compiler should not warn when a broader level of access control is used within a type with more restrictive access, such as
internal
within aprivate
type. This allows the owner of the type to design the access they would use were they to make the type more widely accessible. (The members still cannot be accessed outside the enclosing lexical scope because the type itself is still restricted, i.e. outside code will never encounter a value of that type.)
So, although it doesn't change the accessibility of the members, it allows developers to communicate the access level they feel a given member should have if the enclosing type had a broader access level – which could for example be useful for APIs that currently have internal
types which are planned to be made public
in a future release.
Upvotes: 9
Reputation: 15706
The Swift reference says under the Guiding Principle of Access Levels,
No entity can be defined in terms of another entity that has a lower (more restrictive) access level.
So, I suppose that doesn't mean that an entity can't be defined in terms of another entity that has a higher access level. In fact, that would surely be necessary.
Upvotes: 0