Why am I allowing method access less restrictive than class access?

Why is this compiling?

internal class A { public func f() { } } 

I expected the f "public" modifier to be forbidden because its spanning class is internal.

+5
source share
2 answers

One motivation for this is mentioned in SE-0025: Access Level with Limited Access (highlighted by me):

The compiler should not warn when a wider level of access control is used in a type with more restrictive access, for example, internal in the private type. This allows the type owner to create access that they will use to make them more accessible. (Elements still cannot be accessed outside the covering lexical region, since the type itself is still limited, i.e. the external code will never encounter a value of this type.)

Thus, although it does not change the accessibility of members, it allows developers to communicate the access level that, in their opinion, a given member should have if the type of application has a wider access level, which may, for example, be useful for APIs that are currently have internal types that are planned to be made public in the next version.

+6
source

Swift link in the Access Levels Guideline section,

No entity can be defined in terms of another entity that has a lower (more restrictive) access level.

So, I suppose, this does not mean that the entity cannot be defined in terms of another object with a higher level of access. In fact, this will certainly be necessary.

0
source

Source: https://habr.com/ru/post/1258904/


All Articles