为什么编译?
internal class A { public func f() { } }
我期望f的“公共”修饰符被禁止,因为它的封闭类是内部的.
允许这种情况的一个动机在
SE-0025: Scoped Access Level(强调我的)中提到:
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.)
因此,虽然它不会改变成员的可访问性,但如果封闭类型具有更广泛的访问级别,它允许开发人员传达他们认为给定成员应具有的访问级别 – 例如,这可能对当前具有的API有用.计划在未来版本中公开的内部类型.