[swift-evolution] [Proposal] Property behaviors

Matthew Johnson matthew at anandabits.com
Thu Dec 17 13:12:01 CST 2015


I’ve really been looking forward to this proposal and I like this idea in general.  I may have comments about specific details after giving it more thought.

I have one significant concern that is worth mentioning immediately.  I would consider it extremely unfortunate if a delayed property behavior was considered sufficient in support of multi-phase initialization.  Using a property behavior for that purpose gives no guarantee that the value is actually initialized at some point during instance initialization.  IMO this is a major flaw.  A runtime error might occur when accessing a delayed property after initialization is supposed to be complete.

Delayed property behaviors may have appropriate use cases but IMO they are not an adequate substitute for something that provides stronger guarantees for the common case of multi-phase initialization.

I very strongly prefer to see direct language support for multi-phase initialization.  The compiler could provide most of the initialization guarantees it does for regular let properties.  It could enforce single assignment in the initializer body and could prevent the initializer body itself from reading the delayed property before assignment.  

The only guarantee that may not be possible is method calls to self during the second phase of initialization, but prior to assignment of all delayed properties (whether directly or by passing self to another instance) are potentially dangerous if they caused a read to a delayed property.  The potential for error is significantly narrower with direct language support.  As this is a very common use case (possibly the most common use case for delayed properties) I strongly believe it warrants direct language support.

Matthew


More information about the swift-evolution mailing list