قالب وردپرس درنا توس
Home / IOS Development / Extension Access Control – Erica Sadun

Extension Access Control – Erica Sadun



Swift extends the courtesy of an access control annotated extension to top-level members. I'm going to call this "heritage," but I know it's a better name for this, but I just don't know what it is.

Consider the following:

  // The base type is public
public structure MyStruct {}

// Here the extension is declared public, so every top level member
// "inherit" that access level.
public extension MyStruct {
// This is public even if it is not marked
static was firstValue: String {return "public"}

// This is also public, but the compiler will warn.
public static was secondValue: String {return "public but warned"}

// This class is also public via "heritage"
class PublicSubclass {
// But the members must be marked. This is public
public static la publicValue = "public"
// This default setting is internal
static leave internalValue = "internal"
}
}
In this example,  first level  is inherited  public  access level from  MyStruct  the extension. The explicit annotation for  secondValue  is warned by the compiler as unnecessary. If you treat warnings as wrong, it's a problem.  

Each of the static properties is available outside the module except internalValue which, even in a public class members are not inherited their control level:

Before I start posting a preliminary style guide out there, I would like to point out a few more things about this. Here is another example:

  Internal Class InternalType {}

InternalType extension {
public static value: String {return "value"}
}

Swift collects this code without error. There is clearly a problem with developers. The purpose of making the member public is fundamentally wrong. since it exceeds the type of access control level. This issue is also found outside extensions, where the compiler will not warn of high levels for direct-type members:

  internal class AnotherInternalType {
public was value = "value" // no warning
} 

You can imagine that this is a place where the compiler should beat his game, no? This is a code point that is technically functional and compiled, but whose specification submits the documenting nature using access control. Shouldn't the notice be limited and warned here?

The compiler will find error connections between the expansion ACL and the type ACL:

And this is where the problem comes in because the guide I work with says: "Do not notice about extensions with access control levels except when working with trivial tools ". Skip Extension ACL ensures that you intentionally and intentionally add access control to each member declared within that extension. Each access level is co-located with the declaration it decorates. This allows your code to be more easily revised, and the access levels will immediately appear as intent and implementation.

What are your thoughts? Can you think of some reasons why extensions should be ACL in production code? And this is just a bug / language enhancement thing or something I'm missing. Thanks in advance for feedback.


Source link