Override implicit default access modifier as fileprivate per file

All entities in your code (with a few specific exceptions, as described later in this chapter) have a default access level of internal if you don’t specify an explicit access level yourself. As a result, in many cases you don’t need to specify an explicit access level in your code.
https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html

I was reading the Scoped Conformances Pitch that talks about how conformance access control can not be "controlled" currently swift. So the proposal is to introduce the ability to mark conformances with an explicit access control like below sample code:

public struct X: fileprivate Wrapper,
  Comparable, CustomStringConvertible
{
  /*internal*/ var value: SomeType
}

I also saw a tweet about how you can "trick" the compiler to make a symbol available to the whole module with out having to write import in every file of that module. Example:

// File A in Module AAA
import SomeModule
/*internal*/  typealias Foo = SomeModule.Foo
// File B in Module AAA
let some = Foo() // Foo is exposed to AAA here with out importing the module SomeModule.

The type alias is working as intended because the default access is internal. I think internal is a good default but there are times when it would be helpful to be able to lock everything down in a file specially in the highlighted cases above.

I propose we introduce a way to override the implicit default access modifier per file using a compiler directive:

#fileprivateAsDefaultAccessModifier()

import SomeModule
/*fileprivate*/  typealias Foo = SomeModule.Foo

#fileprivateAsDefaultAccessModifier()

public struct X: /*fileprivate*/ Wrapper,
  /*fileprivate*/ Comparable, /*fileprivate*/ CustomStringConvertible
{
  /*fileprivate*/ var value: SomeType
}

Of course the name can be changed but I am only proposing we should be able to lower the default and not able able to set the default as public for example. Also I don't think we should make it private as this would create confusion if the private is scoped to the toplevel or mimic the scope declaration level. For that case I think if there is enough interest this can be another proposal so it is out of scope for this pitch.

1 Like
Terms of Service

Privacy Policy

Cookie Policy