Good point about unwrapped optionals, but there's no compiler checks to make 
sure they get initialized.

The "defer init" solves the same problem that "defer" within functions 
resolved. There's no need to duplicate the function call in every init, or 
forget to call one as new initializers are added. 

> On Apr 28, 2016, at 12:50 PM, Vladimir.S <sva...@gmail.com> wrote:
> 
> I think I like this idea. It is clear that it is init() and 'defer' says that 
> it is called at the end of each init. IMO we need exactly some kind of 'init' 
> as only in init we can set un-initialized stored properties.
> 
> But, why implicitly unwrapped optionals are not solution here? I.e.
> 
> private var videoPlayer: AVPlayer!
> private var videoPlayerLayer: AVPlayerLayer!
> 
> 
>> On 28.04.2016 19:04, Basem Emara via swift-evolution wrote:
>> I see what you’re saying and the forced optionals is pretty inconvenient.
>> 
>> As far as syntax, how about more of a “deferred” init that gets triggered 
>> regardless like this:
>> 
>> defer init() {
>>    // Always gets called no matter what designated init triggers
>> }
>> 
>>> On Apr 27, 2016, at 5:52 PM, Shannon Potter via swift-evolution 
>>> <swift-evolution@swift.org> wrote:
>>> 
>>> Consider a relatively-common init pattern:
>>> 
>>> class SomeViewController: UIViewController {
>>> 
>>>   // MARK: Properties
>>> 
>>>   private var videoPlayer: AVPlayer
>>>   private var videoPlayerLayer: AVPlayerLayer
>>> 
>>>   // MARK: - Object Lifecycle
>>> 
>>>   override init(nibName: String?, bundle nibBundle: NSBundle?) {
>>>       super.init(nibName: nibName, bundle: nibBundle)
>>> 
>>>       commonInitialization()
>>>   }
>>> 
>>>   required init?(coder decoder: NSCoder) {
>>>       super.init(coder: decoder)
>>> 
>>>       commonInitialization()
>>>   }
>>> 
>>>   private func commonInitialization() {
>>>       videoPlayer = AVPlayer(...)
>>>       videoPlayerLayer = AVPlayerLayer(player: videoPlayer)
>>>   }
>>> 
>>> }
>>> 
>>> This does not work. Both properties are non-optional, and the compiler 
>>> complains that they are not initialized in either init method. It seems 
>>> rather common to want a single point of contact regarding object 
>>> initialization, regardless of the path taken to initialize that object. 
>>> Ideally, objects could all be funneled to one designated initializer, but 
>>> this isn’t always the case.
>>> 
>>> What are people’s thoughts about either a specialized function that is 
>>> always called at the very end of each object’s lifecycle OR some sort of 
>>> attribute for a function that hints that the compiler should follow it if 
>>> called in an init function to check for property initialization?
>>> 
>>> func commonInit() {
>>> 
>>> }
>>> 
>>> or
>>> 
>>> @extend_init private func commonInitialization() {
>>> 
>>> }
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution@swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> 

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to