[swift-evolution] [Proposal] Property behaviors

Matthew Johnson musical.matthew at mac.com
Wed Jan 20 17:26:49 CST 2016


> On Jan 20, 2016, at 5:21 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> 
>> On Jan 20, 2016, at 3:19 PM, Joe Groff <jgroff at apple.com <mailto:jgroff at apple.com>> wrote:
>> 
>> 
>>> On Jan 20, 2016, at 3:17 PM, Chris Lattner <clattner at apple.com <mailto:clattner at apple.com>> wrote:
>>> 
>>> 
>>>> On Jan 20, 2016, at 2:08 PM, Joe Groff <jgroff at apple.com <mailto:jgroff at apple.com>> wrote:
>>>> 
>>>>>>> One thought is that it might be good to allow behaviors to have `init` accessor that is used if the property is assigned by an initializer of the containing type (only the first time the property is assigned).  This would clarify what happens during initialization of the containing type and allow for different init and set code paths when necessary.   It would be distinguished from the behavior initializer by the lack of parens.  If that is too subtle we could use a different name for the initialization accessor.
>>>>>> 
>>>>>> That's a possibility, but inserting initializer calls is a bit more heroic than what our current definite initialization implementation can do. That's not necessarily a showstopper, but it was a goal of the current design to avoid inserting accessor calls within inits at hard-to-predict places. (cc'ing ChrisL, to get his opinion on this as DI owner)
>>>>> 
>>>>> Makes sense.  Getting the behavior I would like to see out of a `phase2Immutable` behavior would definitely require updates to the DI rules.  I think the added safety makes it worth doing something in this area eventually, but it could be added later.  I’m interested to hear what Chris thinks.
>>>> 
>>>> I think it's important that delayed initialization also accommodate initialization *after* init() returns. There are many cases where full initialization depends on a process outside of init's control, especially in Cocoa with objects and outlets set up from nib files.
>>> 
>>> I don’t really consider that to be initialization in the current sense.  IMO, without extending our DI model, a “delayed” behavior for a property of type T is best modeled with storage for T?, which is implicitly initialized to nil.  The getter would do a force unwrap of the optional, and the setter would check that the storage is nil before assigning over it.
>>> 
>>> If/when we have support for a more dynamic initialization (e.g. dynamic typestate) then we could expand this to use it, but I don’t see how it would provide a functionally different model.
>> 
>> This is pretty much what I mean, and what the [delayed] example from the proposal provides. Matthew's making a case for a more constrained form of 'delayed' that still prevents you from exiting 'init' without having initialized everything.
> 
> Ah ok.  Well, such a thing could be built.

Yes, the use case for this is when you need to pass `self` to the initializer of one of your members.  You can’t do this until phase 2, but it would be best if the compiler can still provide as much safety guarantee as possible.

-Matthew 

> 
> -Chris
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160120/c0ce1d74/attachment.html>


More information about the swift-evolution mailing list