<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Yeah, thinking this one through… The use case below also mentioned the possibility of a setup function that had parameters to base it’s setup on. A lazy loading variable doesn’t necessarily cleanly replace that sort of usage without adding more variables to hold the parameters for lazy initialization… But I’m not sure if that’s realistic. Anything that’s part of an initWithCoder serialization /deserialization pattern is probably going to be a variable on the object anyway. Maybe someone else can come up with a better counterexample.<div class=""><br class=""></div><div class="">At least this list is a great way to touch on all the different corners of Swift. :)<br class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 15, 2015, at 6:14 PM, Marc Knaup &lt;<a href="mailto:marc@knaup.koeln" class="">marc@knaup.koeln</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Well lazy vars actually can refer to each other during their initialization.<div class="">Just make sure you don't create any cycles or else you'll end up in an endless recursion.</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Dec 16, 2015 at 3:06 AM, Marc Knaup <span dir="ltr" class="">&lt;<a href="mailto:marc@knaup.koeln" target="_blank" class="">marc@knaup.koeln</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><font face="monospace, monospace" class="">initWithCoder(_:)</font> is exactly such an example you can solve using initialization closures and/or lazy vars I mentioned earlier.<div class=""><br class=""><div class="">The only limitation would be that properties must be initialized independently from each other.</div></div><div class="">If they really depend on each other then using a struct for them probably makes sense since that can be initialized as a whole.</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Dec 16, 2015 at 3:00 AM, Colin Cornaby <span dir="ltr" class="">&lt;<a href="mailto:colin.cornaby@mac.com" target="_blank" class="">colin.cornaby@mac.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">In UIKIt a view can have two primary methods of initialization: initWithCoder and initWithFrame. initWithCoder is special in that it will not call up to initWithFrame, while any other initializer will use initWthFrame as it’s base initializer.<div class=""><br class=""></div><div class="">A common issue is that you might want to set defaults based on runtime information, so you define a common “setup” function that each one of the initializers calls. Again, I haven’t run into this case yet in Swift, but my understanding is that this would not work because the initializers would not be able to call a function on themselves until initialization was complete.</div><div class=""><br class=""></div><div class="">There is one possible way out. NIB outlets are declared implicitly unwrapped to work around this exact problem. A NIB can’t be loaded until after the object’s initialization is complete, and therefore the outlet properties of the object can’t be populated at init time. Any properties that need to have common setup between the two initializers could be declared as implicitly unwrapped. You’d lose some safety, but any issues could probably be quickly caught. This would work around the problem of needing to populate all the properties of the object before calling to the super’s init.<br class=""><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class=""><div class=""><div class="">On Dec 15, 2015, at 5:51 PM, Marc Knaup via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class=""></div></div><div class=""><div class=""><div class=""><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">Although I understand your concerns (Charles and Ross) I think an extensive example is still necessary to properly discuss this proposal. We'd have to figure out what exactly causes the limitation, if there are alternatives and if there is no alternative which would render the change unnecessary.<div class="">So far this is a very theoretical discussion.<div class=""><br class=""></div></div></div><div class="gmail_extra" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br class=""><div class="gmail_quote">On Wed, Dec 16, 2015 at 2:48 AM, Charles Srstka<span class="">&nbsp;</span><span dir="ltr" class="">&lt;<a href="mailto:cocoadev@charlessoft.com" target="_blank" class="">cocoadev@charlessoft.com</a>&gt;</span><span class="">&nbsp;</span>wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><span class=""><blockquote type="cite" class="">On Dec 15, 2015, at 7:06 PM, Marc Knaup &lt;<a href="mailto:marc@knaup.koeln" target="_blank" class="">marc@knaup.koeln</a>&gt; wrote:<br class=""></blockquote><div class=""><blockquote type="cite" class=""><br class=""><div class=""><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important" class="">In its current state with the initial example -1 from me for the proposal.</span><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">The example could easily be written like this:</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><div class=""><font face="monospace, monospace" class="">class FooView: UIView {</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="">&nbsp;</span>var property = 4</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="">&nbsp;</span>init() {</font></div><div class=""><span style="font-family:monospace,monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="">&nbsp;</span>super.init()</span><br class=""></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="">&nbsp;</span>}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="">&nbsp;</span>init(frame: CGRect) {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="">&nbsp;</span>super.init(frame)</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="">&nbsp;</span>}</font></div><div class=""><font face="monospace, monospace" class="">}</font></div></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">In cases where the initially value is computed in a complex way a closure can be used:</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><div class=""><font face="monospace, monospace" class="">class FooView: UIView {</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="">&nbsp;</span>var property: Int = {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="">&nbsp;</span>// some complicated computation</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="">&nbsp;</span>return value</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="">&nbsp;</span>}()</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="">&nbsp;</span>init() {</font></div><div class=""><span style="font-family:monospace,monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="">&nbsp;</span>super.init()</span><br class=""></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="">&nbsp;</span>}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="">&nbsp;</span>init(frame: CGRect) {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="">&nbsp;</span>super.init(frame)</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp;<span class="">&nbsp;</span>}</font></div><div class=""><font face="monospace, monospace" class="">}</font></div></div></div></blockquote></div><br class=""></span><div class="">That works for simplistic cases such as this. However, if you have a large number of properties that all need to be set, and if their values are all interconnected in some way, dependent on some of the initializer’s parameters, or dependent on some value computed at runtime, this pattern cannot accomplish the task cleanly.</div><span class=""><font color="#888888" class=""><div class=""><br class=""></div><div class="">Charles</div><div class=""><br class=""></div></font></span></div></blockquote></div><br class=""></div></div></div><span class=""><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=OFgvmg1J6naJevMotmPmRgzA4p1gcL5s2O89Xj3c0DSwsyiayQ-2BU6ptgtkJ9UKnj0wyGanhKGGyPBy3QQ22hnbRPNNkYkuJY1fdcwonbjByE3Syg5YUecx8bdsv-2FOlvJ88j7KyOVN97JYAaPdMbXDLzbR-2B7cFhFhPAoX5utdt2eyDWi-2F2U8VVVj4949FbbRiurFg37wZzLUD9smjna-2BblETPXRn77p661q-2FiIV-2FGb-2F8-3D" alt="" width="1" height="1" border="0" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important" class=""><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important" class=""><span class="">&nbsp;</span>_______________________________________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important" class="">swift-evolution mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><a href="mailto:swift-evolution@swift.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank" class="">swift-evolution@swift.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div>
</div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></body></html>