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

Matthew Johnson matthew at anandabits.com
Thu Jan 7 12:22:18 CST 2016


> On Jan 7, 2016, at 12:14 PM, Joe Groff <jgroff at apple.com> wrote:
> 
>> 
>> On Jan 7, 2016, at 10:03 AM, Matthew Johnson <matthew at anandabits.com <mailto:matthew at anandabits.com>> wrote:
>> 
>> 
>>> On Jan 7, 2016, at 11:39 AM, Joe Groff <jgroff at apple.com <mailto:jgroff at apple.com>> wrote:
>>> 
>>> 
>>>> On Jan 7, 2016, at 8:28 AM, Dave Abrahams <dabrahams at apple.com <mailto:dabrahams at apple.com>> wrote:
>>>> 
>>>> The latter I'm afraid. 
>>> 
>>> I was just discussing this design space with Chris Willmore, who's been working on revamping how our function type model works. If we move to a multiple-argument model for functions rather than the current every-function-takes-a-tuple-argument model, then we will likely need at least limited support for packing and unpacking tuples from and to arguments in order to avoid regressing at argument forwarding use cases. However, even that limited packing/unpacking functionality might be enough to seriously consider a more general magic "members" property as an alternative.
>> 
>> I don’t mind discussing an alternative using this approach.  If we’re going to do that I think it must be clear how it would cover various intended use cases in detail.  Specifically, how would we address:
>> 
>> 1. Default parameter values (at least for `var` properties)
>> 2. `let` properties: it seems pretty magical indeed if the computed `var` property exposing the tuple could be used to initialize a `let` property.
> 
> Yeah, this member would need special initialization abilities, I agree.

It seems odd that this would be deemed acceptable.  Would the `members` member be usable outside of an initializer?  It seems like that wouldn’t be possible if there were a `let` property.  But it also seems weird if its availability was determined by whether you have a `let` property or not.

> 
>> 3. Partial memberwise initialization exposing a subset of members following some kind of “automatic” or “opt-in” model for determining the subset.
> 
> Seems to me that could be done by factoring the interesting subsets into structs, e.g.:
> 
> class Foo {
>   internal struct MemberwiseProperties {
>     var x,y,z: Int
>   }
>   internal var state: MemberwiseProperties
> 
>   init(members...: MemberwiseProperties.Members) {
>     state.members = members
>   }
> }

Sure you could do that, but it doesn’t seem like an acceptable alternative to me.  Now the members must be prefixed with an identifier everywhere they are used.  I don’t see why this is acceptable when requiring `self` is not.

Are there compelling reasons to go down this road rather than the current proposal?  What use cases does it address that the current proposal does not?


> 
> -Joe
> 
>> 
>> Is it correct to assume that part of the magic is an understanding of the magic properties that allows them to be used to initialize the properties corresponding to the tuple members?
>> 
>> One thing I think is worth considering is that there is magic required no matter what approach we adopt.  The primary advantage of this approach seems to be that the magic / implicit properties might sometimes be useful outside of an initialization context.  There could be other advantages depending on the details of what this approach looks like but its hard to tell without more specifics.
>> 
>> Matthew
>> 
>>> 
>>> -Joe
>>> 
>>>> Sent from my moss-covered three-handled family gradunza
>>>> 
>>>> On Jan 6, 2016, at 7:12 PM, Matthew Johnson <matthew at anandabits.com <mailto:matthew at anandabits.com>> wrote:
>>>> 
>>>>> 
>>>>> 
>>>>> Sent from my iPad
>>>>> 
>>>>> On Jan 6, 2016, at 8:46 PM, Dave Abrahams <dabrahams at apple.com <mailto:dabrahams at apple.com>> wrote:
>>>>> 
>>>>>> 
>>>>>> 
>>>>>> Sent from my moss-covered three-handled family gradunza
>>>>>> 
>>>>>> On Jan 6, 2016, at 5:47 PM, Joe Groff <jgroff at apple.com <mailto:jgroff at apple.com>> wrote:
>>>>>> 
>>>>>>> 
>>>>>>>> On Jan 6, 2016, at 5:23 PM, Matthew Johnson via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>>>>> 
>>>>>>>>> 
>>>>>>>>> On Jan 6, 2016, at 6:04 PM, Dave Abrahams via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>>>>>> 
>>>>>>>>> 
>>>>>>>>>> On Jan 6, 2016, at 2:47 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org <mailto: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 <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 <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?
>>>>>>>>> 
>>>>>>>>> It’s okay.
>>>>>>>>> 
>>>>>>>>>> 	* Is the problem being addressed significant enough to warrant a change to Swift?
>>>>>>>>> 
>>>>>>>>> I’m lukewarm about that.  I have never found writing out the initializers I want to be a significant burden, and I find my code is better when they’re explicit.  Every new feature increases the language's complexity and surface area, and I fear this one is not going to pay its way.
>>>>>>>>> 
>>>>>>>>>> 	* Does this proposal fit well with the feel and direction of Swift?
>>>>>>>>> 
>>>>>>>>> Yes, but I worry that it may be too early to add it.  Other features in this space, like truly generic variadics, may well obsolete anything we do today.  I’m not sure we should be designing convenience features that are likely to overlap with more general features coming down the road unless the inconvenience is very painful… which I personally don’t find it to be.
>>>>>>>> 
>>>>>>>> It isn’t clear to me how generic variadics might obsolete the functionality of this proposal.  Can you elaborate on that?
>>>>>>> 
>>>>>>> Not sure if this is exactly what Dave has in mind, but an idea that comes to mind: we could say that structs and classes have a magic "members" tuple and typealias:
>>>>>>> 
>>>>>>> struct Foo {
>>>>>>>   var x, y: Int
>>>>>>> 
>>>>>>>   // Implicit members
>>>>>>>   typealias Members = (x: Int, y: Int)
>>>>>>>   var members: Members {
>>>>>>>     get { return (x: x, y: y) }
>>>>>>>     set { (x, y) = newValue }
>>>>>>>   }
>>>>>>> }
>>>>>>> 
>>>>>>> With plausible future features for forwarding tuples as arguments, then the memberwise initializer could be implemented like this:
>>>>>>> 
>>>>>>> 	    // Say that a parameter declared 'x...' receives a tuple of arguments labeled according to its type,
>>>>>>> 	    // like '**x' in Python
>>>>>>> init(members...: Members) {
>>>>>>>   self.members = members
>>>>>>> }
>>>>>>> 
>>>>>>> And I think all your other use cases could be covered as well.
>>>>>> 
>>>>>> That's exactly what I had in mind. Thanks, Joe!
>>>>> 
>>>>> Is there any chance of generic variadics along these lines being part of Swift 3?  Or is this down the road further?

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


More information about the swift-evolution mailing list