[swift-users] default struct initializer internal even if class declared public

Shawn Erickson shawnce at gmail.com
Wed Jan 18 10:44:57 CST 2017


Yeah I am fairly sure that is by design. A lot of swifts access controls
are about getting you up and going with little work / boilerplate while
internal to your model while requiring you to be explicit about what you
want to expose publicly outside of your module.

On Wed, Jan 18, 2017 at 8:40 AM Adrian Zubarev via swift-users <
swift-users at swift.org> wrote:

I feel like I’ve seen this discussion somewhere on the mailing list before.
If I remember correctly or it could be only me, this behavior is by design,
because you don’t want to open your API implicitly to everyone. Internally
it won’t hurt your module, but only allow you to write less code and use
the type right away.




It might be your intention of using this init(firstName:lastName) only
internally, but disallow the module user from being able to construct that
type manually. (The current behavior.)




Please feel free to correct me if I’m wrong here ;)






-- 
Adrian Zubarev
Sent with Airmail

Am 18. Januar 2017 um 16:33:22, Dave Reed via swift-users (
swift-users at swift.org) schrieb:

I’m teaching an iOS with Swift this semester and one of my students pointed
out that:

struct Person {
var firstName: String
var lastName: String
}

does create a default initializer that you can call as:
p = Person(firstName: “Dave”, lastName: “Reed”)

but if you write:

public struct Person {
var firstName: String
var lastName: String
}

The default initializer is still internal so if you want it to be public,
you have to write it yourself (i.e.)

public struct Person {
var firstName: String
var lastName: String

public init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
}

Is there a way around this (other than writing it)? We both agree it would
be reasonable/nice that the default initializer have the same protection
level as the struct itself.

Thanks,
Dave Reed

_______________________________________________
swift-users mailing list
swift-users at swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________


swift-users mailing list


swift-users at swift.org


https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170118/795b9270/attachment.html>


More information about the swift-users mailing list