Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

swift - Why am I allowed method access less restrictive than class access?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 a private 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...