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

Matthew Johnson matthew at anandabits.com
Fri Jan 8 09:55:50 CST 2016


> On Jan 7, 2016, at 8:30 PM, Joe Groff <jgroff at apple.com> wrote:
> 
> 
>> On Jan 7, 2016, at 11:30 AM, Matthew Johnson <matthew at anandabits.com <mailto:matthew at anandabits.com>> wrote:
>> 
>> 
>>> On Jan 7, 2016, at 12:51 PM, Joe Groff <jgroff at apple.com <mailto:jgroff at apple.com>> wrote:
>>> 
>>> 
>>>> On Jan 7, 2016, at 10:49 AM, Matthew Johnson <matthew at anandabits.com <mailto:matthew at anandabits.com>> wrote:
>>>> 
>>>>> 
>>>>> On Jan 7, 2016, at 12:37 PM, Joe Groff <jgroff at apple.com <mailto:jgroff at apple.com>> wrote:
>>>>> 
>>>>> 
>>>>>> On Jan 7, 2016, at 10:32 AM, David Owens II <david at owensd.io <mailto:david at owensd.io>> wrote:
>>>>>> 
>>>>>> And this is more clear than this?
>>>>>> 
>>>>>> class Foo {
>>>>>>   var x,y,z: Int
>>>>>>   init(x: Int, y: Int, z: Int) {
>>>>>>     self.x = x
>>>>>>     self.y = y
>>>>>>     self.z = z
>>>>>>   }
>>>>>> }
>>>>> 
>>>>> No, it isn't, but Matthew asked… I'm personally not too motivated to support anything more than all-or-nothing memberwise initialization, and tend to agree that anything more specialized deserves an explicit implementation.
>>>> 
>>>> Maybe you would feel differently if you were an app developer.  Different kinds of code have different needs.  The most important use cases I have in mind are related to UI code, which is often the majority of the code in an app.
>>> 
>>> Do you have any concrete examples in mind?
>>> 
>>> -Joe
>>> 
>> 
>> Here is an example where partial memberwise initialization would apply.  It is similar to something in a real project:
>> 
>> public class FontPicker: UIControl {
>>   public let fonts: [UIFont]
>> 
>>   public var fontSize: CGFloat = 22
>>   public var foregroundColor: UIColor = UIColor.darkGrayColor()
>>   public var backgroundColor: UIColor = UIColor.whiteColor()
>>   // A bunch of other appearance attributes here
>> 
>>   private let collectionView: UICollectionView
>>   private let layout: UICollectionViewLayout
>>   // other internal state required by the implementation
>> 
>>   public memberwise init(...) {
>>     // configure the collection view and add it as a subview
>>   }
>> }
>> 
>> A couple points are relevant here:
>> 
>> 1. Memberwise initialization is very valuable for the appearance attributes, but is useless if it exposes our implementation details.
>> 
>> 2. In many custom UI widgets the appearance attributes don’t really need to be mutable post-initialization.  At the same time, it is necessary to allow, but not require a value to be specified.  It would be ideal if they were `let` properties with a default value, but still able to participate in memberwise initialization.  Without that capability we are forced to choose between the advantages of using a `let` property and the advantages of memberwise initialization.
>> 
>> UI widgets are a great example.  View controllers often have a similar divide between state provided by the user and state related to internal implementation details.
> 
> Access control seems like a poor tool for the kind of categorization you want here. The vast majority of code is app code, where there's no reason to use 'public', so 'internal' and 'private' are the interesting visibility layers. Using 'private' to opt fields out of memberwise initialization is too brittle, in my opinion—You've made it much harder to factor the class's functionality into different files in the future, since you can no longer change any of these fields to internal without also breaking all of the memberwise initializers as a second-order effect.

One thing I should have mentioned in the reply last night is that the problem of breaking the memberwise initializers when access control changes is solvable.  Several of the possible enhancements are attempts to allow the programmer to be more explicit when the rules don’t do the right thing.  The breakage you describe is an example where that would be necessary.

The solution I believe is the best one (assuming the automatic model) is allowing distinct access control for initialization.  Initialization is conceptually distinct from both getting and setting.  We already allow programmers to specify distinct access levels for `get` and `set` (for `var`), but we don’t for `init`.  It feels like a natural extension of the access control model to allow a distinction for initialization.  The `init` access level could also be used in phase 1 for manually written initializers, rather than the `get` visibility of a `let` property and the `set` visibility of a `var` that is used today.

It would solve the problem problem you mention above by allowing the access control to be specified as `internal private(init)` rather than just `internal`.  This access control change would have no impact on memberwise initializers.

This would also solve the problem you mentioned earlier that `let` properties more capable than `private(set) var` properties.  It would no longer be necessary to use the `set` access level because there would be a distinct access level for `init`.  In that case, the programmer could specify exactly what is desired: 

1. `public private(set)` would allow public memberwise initializers to synthesize a parameter for the property
2. `private public(get)` would only allow private memberwise initializers to synthesize a parameter for the property.  Only the getter would be visible publicly.
3. `public private(init)` would only allow private memberwise initializers to synthesize a parameter for the property. Both the getter and setter would be visible publicly.

Ultimately, I think the automatic model is only a good idea if we enforce access control.  Allowing the distinction to be made solves the problems you have pointed out while also allowing the programmer to get the desired behavior out of the automatic model in most or all cases.  For those reasons I think it would be a significant improvement over the current proposal.

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


More information about the swift-evolution mailing list