<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 6. Oct 2017, at 14:49, Spencer Kohan via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi all,<div class=""><br class=""></div><div class="">In migrating to swift 4 I've noticed one thing which has changed is there is no longer support for the automatic re-initialization of lazy variables.&nbsp;&nbsp;</div><div class=""><br class=""></div><div class="">In swift 3 I could declare a variable like this:</div><div class=""><br class=""></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; lazy var myVar : MyType! = {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; return makeMyVar()</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; }()</font></div><div class=""><br class=""></div><div class="">And then if I wanted that initialization block to be executed again, I could <font face="arial, helvetica, sans-serif" class="">just nullify the variable:</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; self.myVar = nil</font></div><div class=""><br class=""></div><div class="">It looks as if this was never intentional, and according to <a href="https://bugs.swift.org/browse/SR-5172" class="">this bug report</a>, and now the nullified variable stay null rather than executing the initialization block a second time.</div><div class=""><br class=""></div><div class="">Has the possibility been discussed of providing a way to force lazy vars to initialize more than once?&nbsp; Working with swift 3 I found this behavior to be very useful in some use cases.&nbsp; For example in computer graphics it's a nice way to handle the generation of assets which only need to be created in the case of some relatively infrequent event, like for instance a window resize.&nbsp; It seems I'm not alone - the<a href="https://stackoverflow.com/questions/25072597/re-initialize-a-lazy-initialized-variable-in-swift" class=""> top stack-overflow question</a> related to re-initializing lazy variables has over 10k views, so I suppose others have found a use case for this functionality as well.</div><div class=""><div class=""><div class=""><br class=""></div>Cheers,
</div></div><div class="">Spencer Kohan</div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""><div class="">Yes, the issue has come up before. There is/was a plan to generalise “lazy” as some kind of “property behaviour” which could support more advanced functionality, such as you described. AFAIK it doesn’t have a specific place on the roadmap.</div><div class=""><br class=""></div><div class="">See:&nbsp;<a href="https://github.com/apple/swift/blob/master/test/Prototypes/property_behaviors/lazy.swift" class="">https://github.com/apple/swift/blob/master/test/Prototypes/property_behaviors/lazy.swift</a></div><div class=""><br class=""></div><div class="">- Karl</div></body></html>