Dangerous Issues: Entry Management Extension

Swift extends the courtesy of an annotated extension of entry management to its top-level members. I’ll name it a "legacy", however I do know his title is healthier, however I simply have no idea what it’s.

Think about the next:

// The fundamental kind is public
public construction MyStruct

// Right here, the extension is said public, so every member of the very best stage
// "inherit" from this stage of entry.
public extension MyStruct
// That is public even when it isn’t annotated
static var firstValue: String return "public"

// That is additionally public however the compiler will warn you.
public static var secondValue: String return "public however warned"

// This class can be public by way of "inheritance"
PublicSubclass class

On this instance, firstValue inherits from the general public entry stage of MyStruct extension. The express annotation of secondValue is reported by the compiler as pointless. Should you deal with warnings as errors, that's an issue.

Every of the static properties is accessible exterior the module, except for internalValue, as a result of even in a public class declaration, its members don’t inherit its stage of management:

Earlier than starting to formulate preliminary pointers on type, I wish to level out just a few further factors on this topic. Here’s a second instance:

inner class InternalType

InternalType extension

Swift compiles this code with out error. That is clearly an issue associated to the builders. The intention to make the member public is basically flawed. as a result of it exceeds the extent of entry management of the sort. This downside additionally exists exterior extensions, the place the compiler won’t announce at too excessive ranges for direct kind members:

inside class AnotherInternalType

You’d think about that it is a place the place the compiler ought to enhance his recreation, proper? It’s a technically purposeful and compilable code level whose specs reduce the documentary nature of using entry management. Shouldn’t the annotation be restricted and warned right here?

The compiler will discover inconsistencies between the LCD extension and the ACL kind:

That's the place the issue lies, as a result of the directions I'm engaged on learn: "Don’t write extensions with entry management ranges, besides while you're utilizing trivial utilities." Ignoring the ACL extension ensures which you could add considerably and deliberately an entry management to every member declared on this extension. Every stage of entry is co-located with the assertion that it decorates. This facilitates the auditing of your code and its entry ranges will seem instantly within the intention and implementation.

What are your ideas? Are you able to consider explanation why extensions ought to all the time seem in ACL within the manufacturing code? And is it only a bug or a language enchancment or is there one thing lacking? Thanks prematurely to your feedback.