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

Kevin Nattinger swift at nattinger.net
Wed Jan 18 10:39:50 CST 2017


I ran into this issue not half an hour ago; I would also prefer the default initializer to default to the entity’s access level, or at least have some simple way of opting in.

> On Jan 18, 2017, at 3:33 PM, Dave Reed via swift-users <swift-users at swift.org> wrote:
> 
> 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



More information about the swift-users mailing list