[swift-evolution] [Review] SE-0018 Flexible Memberwise Initialization

plx plxswift at icloud.com
Thu Jan 7 09:02:01 CST 2016

I like the general notion of something along these lines but admittedly haven’t had time to dig into the proposal specifics yet.

I have some concerns about cross-interactions with other features that are either also under discussion or are at least very anticipatable.

First, I know there has already been some discussion of allowing definition of stored fields in (some) extensions (e.g., something like allowing definition of stored fields in extensions within the module that defines the type).

E.G., something like this may become possible (assume all files are compiled together):

    // in `ComplicatedClass.swift`
    class ComplicatedClass {
      let text: String
      // how will this get expanded,
      // given the extensions below?
      memberwise init(...)
    // in `ComplicatedClass+Foo.swift`
    extension ComplicatedClass {
      var fooData: Foo? = nil
      // presumably stuff-involving-`fooData`
    // in `ComplicatedClass+Bar.swift`
    extension ComplicatedClass {
      var barData: Bar = Bar.standardBar
      // presumably stuff-involving-`barData`

It doesn't seem impossible to specify how the memberwise-initialization would interact with constructs like the above, but I'd worry a bit about it making a feature that's already looking *rather* complicated even more so.

Especially since, if I had to pick just one, I'd think the ability to define stored properties outside the initial definition is a bigger win than a nice memberwise-initialization construct, even though both seem handy.

Secondly, I’m a bit unsure how this will interact with e.g. the property-behavior proposal if both wind up ratified. For `lazy`, the interaction with `memberwise` is easy — it is omitted from the list — but when you get into e.g. something like a hypothetical `logged` or `synchronized` or `atomic` — wherein there is custom behavior, but the field would still need initialization — you’d want them to be included in the `memberwise` init.

It’s a bit unfair to bring up another proposal, but this proposal and something like the property-behavior proposal *would* need to work well together (if both are approved).

Thirdly, I’m not sure what the current plans are (if any) for users to be able to specify the precise memory-layout of a struct; apologies if this is already a feature, I simply haven’t looked into it.

**Today**: I order stored-field declarations for ease-of-reading (e.g. grouped into logical groups, and organized for ease-of-reading).

**Under Proposal**: I sometimes will get to choose between the “ease-of-reading” declaration ordering and the “cleanest-reading memberwise init” declaration ordering. These may not always be identical.

**Future?**: I may have to choose between the “ease-of-reading” declaration ordering, the “cleanest-reading member wise init” declaration ordering, and (perhaps?) the “intended memory-layout” declaration ordering.

I don’t want to make this proposal more-complicated than it already is, but I worry a bit about having too many things impacting the choice of how to order declarations in source files; it may be better to include a way to explicitly declare the ordering-for-memberwise:

E.G., some way of explicitly indicating the memberwise ordering, perhaps like this:

    // syntax example re-using `ComplicatedClass`
    class ComplicatedClass  {
      // ^ can use just @memberwise to get default ordering + the defaults from
      //   the property declarations, but perhaps require the explicit listing
      //   whenver the ordering is not well-defined (e.g. if you have properties
      //   declared in extensions…then you need to order it yourself)
      //   @memberwise(text="Example",barData=,fooData)
      //   - `text="Example"` => memberwise init has text="Example"
      //   - `barData=` => memberwise init has `barData` w/out default
      //   - `fooData` => memberwise init has `fooData` w/default if it has one
      //   …and e.g. the above would make:
      //   memberwise init(...)
      //   ...expand-to:
      //   init(text:String = "Example", barData: Bar, fooData:Foo?=nil)
      //   ...and with the @memberwise declaration supporting a `...` for `super`
      //   placement, like so:
      //   // superclass members come before:
      //   @memberwise(...,)
      //   @memberwise(...,$parameterList)
      //   // superclass members come after      
      //   @memberwise(,...)
      //   @memberwise($parameterList,...)
      //   ...perhaps with tweaked syntax (`@memberwise(...,$)` or `@memberwise(...,self)`)
      //   to be bit easier to read when you don't have an explicit parameter list?

...which of course potentially only-further complicates the feature in some ways, but avoids having this use of this feature *necessarily* impact how one might choose to order declarations?

> On Jan 6, 2016, at 4:47 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
> Hello Swift community,
> The review of "Flexible Memberwise Initialization" begins now and runs through January 10th. The proposal is available here:
> 	https://github.com/apple/swift-evolution/blob/master/proposals/0018-flexible-memberwise-initialization.md
> Reviews are an important part of the Swift evolution process. All reviews should be sent to the swift-evolution mailing list at
> 	https://lists.swift.org/mailman/listinfo/swift-evolution
> or, if you would like to keep your feedback private, directly to the review manager.
> What goes into a review?
> The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift. When writing your review, here are some questions you might want to answer in your review:
> 	* What is your evaluation of the proposal?
> 	* Is the problem being addressed significant enough to warrant a change to Swift?
> 	* Does this proposal fit well with the feel and direction of Swift?
> 	* If you have you used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
> 	* How much effort did you put into your review? A glance, a quick reading, or an in-depth study?
> More information about the Swift evolution process is available at
> 	https://github.com/apple/swift-evolution/blob/master/process.md
> Thank you,
> -Chris
> Review Manager
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

More information about the swift-evolution mailing list