swift – 为什么我允许方法访问比类访问更少限制?

为什么编译?
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 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.)

因此,虽然它不会改变成员的可访问性,但如果封闭类型具有更广泛的访问级别,它允许开发人员传达他们认为给定成员应具有的访问级别 – 例如,这可能对当前具有的API有用.计划在未来版本中公开的内部类型.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...