[swift-evolution] [Proposal Draft] partial initializers

Matthew Johnson matthew at anandabits.com
Tue Jan 12 15:14:53 CST 2016


> On Jan 12, 2016, at 2:23 PM, Brent Royal-Gordon <brent at architechies.com> wrote:
> 
>>> I was about to post this idea (although I was going to suggest `partial func`, and possibly giving the `partial` keyword a parenthesized list of the properties that it expects to be initialized before you call it).
>> 
>> How common do you think it would be for a partial init to need to require that some properties already be initialized.  Do you think that is important right away or do you think it's ok to start without it and enhance the feature later if we run into common needs for it?  I lean towards this as a possible enhancement rather than an immediate requirement.
> 
> I think that many, possibly most, partial methods—especially those that are meant to be called as normal methods will need to access existing properties

Do you have some concrete examples?  I’m sure this would be useful in some cases, I’m just not sure whether we need to include it right away.  

If the memberwise init review is anything to go by, there will be a lot of pushback that something like this makes the proposal too complex.  People might warm to it more after using the basic feature for a while and realizing the limitation this poses in practice.

> , and the compiler will need to make sure partial method calls are ordered such that everything needed is already initialized; it's simply a question of whether we declare what's needed or automatically detect it. I've noticed in previous proposals that scanning the method body is disfavored, so I figure that's not our best bet here.

This is another reason I am not sure about this.  I think you are right that this would require a declaration.  It becomes much more verbose that way.

One thing I would be concerned about is that the compiler will enforce proper order during initialization, but it will not enforce anything later.  If you are resetting state you will be on your own to do things in the correct order.  If the partial inits are not able to read from a property they didn’t write that at least helps prevent mistakes during the reset sequence (of course at the cost of some flexibility in structuring your code).

> 
> (On the other hand, if we don't declare required properties, I guess that might leave `partial(only)` to indicate a method that initializes a constant and thus can't be called except in a designated initializer.)
> 
> -- 
> Brent Royal-Gordon
> Architechies
> 



More information about the swift-evolution mailing list