[swift-evolution] Public struct init is unexpectedly internal

David Sweeris davesweeris at mac.com
Mon Jan 30 03:12:57 CST 2017


So I’ve got this code in a package called “SomeLib":
public struct SomeType {
    public var text = "SomeText"
}
and then, in another package, write this:
import SomeLib
print(SomeType().text)
and then run swift build, I get this error:
error: 'SomeType' initializer is inaccessible due to 'internal' protection level

"Well that’s odd… there isn’t even an initializer to be internal or public”, I said to myself. Then I proceeded to futz around with it for a while before having a lightbulb moment:
public struct SomeType {
    public var text = "SomeText"
    public init() {} //this fixes it
}


In cases like this where the struct is public and all its stored properties are both public and already have values, should we make the implicit init() function also be public? Seems like the “least surprising” thing to do.

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170130/32163f4e/attachment.html>


More information about the swift-evolution mailing list