Consider the follow class (equally applicable to a struct, as well) in a module:
public class Foo {
public func bar() {
// method body
}
}
Note, it does not have an explicit initializer; this example doesn't need any special initialization. This class would be exposed to other modules because it is marked public. However, when code outside the module attempts to initialize it, the compiler complains:
let foo = Foo() // 'Foo' initializer is inaccessible due to 'internal' protection level
In order to satisfy the compiler, I have to define an explicit empty initializer marked public:
public class Foo {
public init() {
// This initializer intentionally left empty
}
public func bar() {
// do something useful
}
}
Why, if the class is explicitly public, do I need to explicitly define a public initializer? Shouldn't it implicitly have a public initializer?
There is a related question here, pertaining to unit testing, but I find it doesn't really get at the core of the design philosophy of what I find to be a surprising issue.