(this is my first pitch, so please don't be too harsh)
Current
Today (Xcode 11.3.1, Swift 5.1.X) any public struct Foo
will get an auto-synthesized initializer (ASI) with access control internal
.
This is tedious when working with models/DTOs and multiple packages (modules), where I might wanna initialize a struct
from another package, however, its auto-synthesized initializer (ASI) is internal.
It is not possible to simple mark/change the access control of this internal initializer to be public, without implementing it myself. Which quite quickly becomes a bit tedious (especially since Xcode lacks IDE function to generate this initializer for me - exists for classes, but not for struct). This "problem" has some traction on StackOverflow (with comment.)
Two solutions
I see two viable solutions.
Change AC of ASI to match AC of struct
The simplest solution is to just change the default access control of this ASI (auto-synthesized initializer) to match the AC of the declared struct. Meaning no change for internal struct Foo
, but would make the ASI of public struct Bar
be public.
Introduce syntax to allow for an explicit declaration of AC of ASI
Or we add some new statement (syntax) which would allow me to change the AC of the ASI of a public struct
to be public
instead of internal
. E.g.
public struct Person {
// some properties
public let dateOfBirth: Date
// example of some new statement to make
// auto-synthesized init public instead of internal
// similiar to `public private(set) var` syntax.
public(init)
}
Discussion
Where the first solution is the simplest and does not require any new statement/syntax, it might not be what one wants, it might be important to disallow for init of models from other modules/packages. This makes the second solution much more interesting.