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

plx plxswift at icloud.com
Fri Jan 8 09:46:59 CST 2016


After reading both your response below and also the proposal rather carefully, I agree that the possible issues I raised are all either not real issues or already addressed; thanks again for crafting the proposal and also for taking the time to reply to so much feedback.

That being said, I can’t shake a feeling that, overall, although I am definitely in favor of something along the lines of this proposal, in its concrete details at present this proposal isn’t really sitting anywhere near even a local-optimum on the `(flexibility,complexity) -> functionality` surface, as it were; it seems like both of these are possible:

- (a) make it a bit more flexible, for a high gain in functionality at a low incremental cost in complexity
- (b) make it a bit less flexible, for a modest loss in functionality and a large drop in complexity

…but for (b) it’s just a feeling and I don’t have a specific proposal (this may well be close to a minimum-viable-proposal for such a feature).

For (a) my sense is that although I can understand why you don’t want to even provide the option of specifying an explicit memberwise-parameter list, it really does seem that supporting at least an optional list makes it possible to get a lot more functionality for not much more *actual* complexity; this isn’t incompatible with also supporting an “automatic” option that uses the logic from the proposal where possible.

Here’s a concrete example to illustrate why I’m harping on this point; I apologize for the length, but I think “small-n” examples can often give false intuition into how things will behave in real life:

class FancyCollectionViewDriver : NSObject, UICollectionViewDataSource, UICollectionViewDelegate /*, etc... */ {
  
  let collectionView: UICollectionView
  let contentPresentation: ContentPresentation  
  let modelBroker: ModelBroker
  let imageBroker: ImageBroker
  let analyticsSink: AnalyticsSink
  private(set) var currentData: ModelData
  private(set) weak var interactionDelegate: DriverDelegateProtocol?
  // ^ can't be non-optional `unowned let` for reasons,
  //   but we expect a non-nil argument in init
  // NOTE: numerous private state-tracking variables omitted since we are only focusing on initialization

  // Present-day initializer, full of boilerplate:
  required init(
    collectionView: UICollectionView, 
    contentPresentation: ContentPresentation,
    modelBroker: ModelBroker,
    imageBroker: ImageBroker,
    analyticsSink: AnalyticsSink,
    // note use of different argument name:
    initialData: ModelData,
    // note use of non-optional:
    interactionDelegate: DriverDelegateProtocol) {
      // oh boy here we go again:
      self.collectionView = collectionView
      self.contentPresentation = contentPresentation
      self.modelBroker = modelBroker
      self.imageBroker = imageBroker
      self.analyticsSink = analyticsSink
      self.currentData = initialData
      self.interactionDelegate = interactionDelegate
      super.init()
      // only non-assignment logic in the entire init:
      self.collectionView.dataSource = self
      self.collectionView.delegate = self
    }
    
    // best we can do under proposal w/out modifying 
    // class design:
    required memberwise init(
    // lots of boilerplate gone:
    ..., 
    // this isn't changed:
    initialData: ModelData,
    // this isn't changed:
    interactionDelegate: DriverDelegateProtocol) {
      // synthesized stuff is effectively here
      self.currentData = initialData
      self.interactionDelegate = interactionDelegate
      super.init()
      // only non-assignment logic in the entire init:
      self.collectionView.dataSource = self
      self.collectionView.delegate = self
    }
  
}

…which I do think is already a huge improvement. 

Now suppose that stored-properties-in-extensions hits (as the "partial-init" flavor); in that case I’d ideally be able to move some of the parts into their own files like so:

// in `FancyCollectionViewDriver+Analytics.swift`
extension FancyCollectionViewDriver  {
  // ^ depending on advances in the protocol system, at some point may 
  //   evolve into a protocol-adoption to get useful default implementations
  
  let analyticsReporter: AnalyticsReporter 
  // ^ moved here, not in main declaration
  //   assume also a bunch of private state-tracking stuff...
  
  // a bunch of things like this:
  func reportEventTypeA(eventAInfo: EventAInfo)
  func reportEventTypeB(eventBInfo: BventAInfo)
  
}

// in `FancyCollectionViewDriver+Interaction.swift`
extension FancyCollectionViewDriver {
  
  private(set) var interactionDelegate: DriverDelegateProtocol?
  // ^ moved here, not in main declaration
  //   assume also a bunch of private state-tracking stuff...
  
  // a bunch of things like this:
  func handleInteractionA(interactionAInfo: InteractionAInfo)
  func handleInteractionB(interactionBInfo: InteractionBInfo)
  
}

…(and so on for e.g. the `imageBroker` also), but under the current proposal this would put them outside the scope of a memberwise init (this isn’t news to you, I’m just making it concrete).

So in this scenario, we’re either reapproaching the MxN problem memberwise-init is meant to avoid:

init(
  // still save some boilerplate:
  …, 
  imageBroker: ImageBroker,
  analyticsReporter: AnalyticsReporter, 
  initialData: ModelData, 
  interactionDelegate: DriverDelegateProtocol) {
  // some boilerplate synththesized here...
  // ...but getting closer to where we started:
  self.partial_init(imageBroker: imageBroker)
  self.partial_init(analyticsReporter: analyticsReporter)
  self.currentData = modelData
  self.partial_init(interactionDelegate: interactionDelegate)  
  super.init()
  self.collectionView.dataSource = self
  self.collectionView.delegate = self
}

…or we’re making choices between taking full-advantage of properties-in-extensions (which IMHO would often be a *huge* readability win) versus taking full-advantage of boilerplate-reduction in our inits.

Which is ultimately why I suspect that the “right" version of the proposed feature should cut to the chase and incorporate some way to explicitly-specify the memberwise parameter list — which, again, need not be incompatible with the ability to request automatic synthesis using logic ~ what’s in the proposal — as such an explicit list takes the pressure off of getting the default behavior as-right-as-possible while also making it simpler to support some very nice-to-have capabilities not supported by this proposal as-written.

That’s my 2c; thanks to anyone who’s read through all this and thanks again for drafting a concrete-enough proposal to discuss properly.

> On Jan 7, 2016, at 9:24 AM, Matthew Johnson <matthew at anandabits.com> wrote:
> 
>> 
>> On Jan 7, 2016, at 9:02 AM, plx via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> 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.
> 
> I followed the stored-properties-in-extensions discussion reasonably closely.  My understanding is that the extension will need to initialize its own properties, either with an initial value or with a `partial init`.  Designated initializers would be required to call the `partial init` for any extension that defines one.
> 
> This being the case, memberwise initialization would not directly interact with this feature at all.  Memberwise initializers declared in the main body of type itself would only expose stored properties defined in the type itself.  
> 
> It would also be possible to support `partial memberwise init` in extensions which would expose the stored properties declared in the extension as part of a partial initializer.
> 
> I don’t think there are difficult complications here.
> 
>> 
>> 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.
> 
> My thought here is that a behavior would define whether a property allows and / or requires initialization in phase 1 or not.  This is probably necessary independent of memberwise initialization.  Properties that allow or require phase 1 initialization would be eligible for memberwise initialization. Properties that don’t allow phase 1 initialization would not be eligible for memberwise initialization.
> 
>> 
>> 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).
> 
> Agreed.  That is why there is a rule that references property behaviors in the proposal.
> 
>> 
>> 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.
> 
> Agree.  This is something that could be addressed in a future enhancement if necessary.  This proposal is focused on the basic mechanism.
> 
> Also, nothing in the proposal prevents you from continuing to write a manual initializer when the synthesized initializer will not do what you require.  If you are already explicitly restating the property identifiers to specify parameter order you are already half way to a manual initializer implementation.  
> 
> Granted, if you need more than one memberwise initializer you would have to duplicate that effort.  But re-ordering is going to have a hard time providing enough value if the basic feature does what we need in the majority of cases.
> 
> 
>> 
>> **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  {
>>     @memberwise($parameterList)
>>     // ^ 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
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160108/ae444791/attachment.html>


More information about the swift-evolution mailing list