[Probe] The automatically generated init for structs


(Daniel Steinberg) #1

My apologies if this was resolved before.

At one point there was a suggestion for the access level for the auto-generated init for structs. The issue was that the access level is internal by default. If you want to make this init public you have to implement the init yourself - there is no syntax for marking the default init as public.

Also, as in many other languages, if you implement your own init the default init is not generated. There are times when you want this to be generated anyway so that other inits might call through it. Perhaps to set default values without having to reimplement this init.

Finally, in this last case you might want the default init generated but not exposed to the module or publicly - you may want to mark it as fileprivate but expose another init that uses it.

The following is horrible syntax but I wonder if there can be something that accomplishes these tasks - perhaps

@defaultInit and

public @defaultInit

private @defaultInit

Best,

Daniel


(Chris Lattner) #2

This topic has definitely come up, culiminating in this proposal:

I expect this to come up again after Swift 3 is done,

-Chris

···

On Jul 1, 2016, at 4:09 AM, Daniel Steinberg via swift-evolution <swift-evolution@swift.org> wrote:

My apologies if this was resolved before.

At one point there was a suggestion for the access level for the auto-generated init for structs. The issue was that the access level is internal by default. If you want to make this init public you have to implement the init yourself - there is no syntax for marking the default init as public.

Also, as in many other languages, if you implement your own init the default init is not generated. There are times when you want this to be generated anyway so that other inits might call through it. Perhaps to set default values without having to reimplement this init.

Finally, in this last case you might want the default init generated but not exposed to the module or publicly - you may want to mark it as fileprivate but expose another init that uses it.


(Daniel Steinberg) #3

Thank you

···

On Jul 1, 2016, at 4:24 PM, Chris Lattner <clattner@apple.com> wrote:

On Jul 1, 2016, at 4:09 AM, Daniel Steinberg via swift-evolution <swift-evolution@swift.org> wrote:

My apologies if this was resolved before.

At one point there was a suggestion for the access level for the auto-generated init for structs. The issue was that the access level is internal by default. If you want to make this init public you have to implement the init yourself - there is no syntax for marking the default init as public.

Also, as in many other languages, if you implement your own init the default init is not generated. There are times when you want this to be generated anyway so that other inits might call through it. Perhaps to set default values without having to reimplement this init.

Finally, in this last case you might want the default init generated but not exposed to the module or publicly - you may want to mark it as fileprivate but expose another init that uses it.

This topic has definitely come up, culiminating in this proposal:
https://github.com/apple/swift-evolution/blob/master/proposals/0018-flexible-memberwise-initialization.md

I expect this to come up again after Swift 3 is done,

-Chris