<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 8, 2016, at 4:17 AM, Greg Parker &lt;<a href="mailto:gparker@apple.com" class="">gparker@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Nov 4, 2016, at 2:57 AM, Anton Mironov via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hi all,<br class=""><br class="">I want to initialize constant object graph with cycles. I've considered two workarounds, but this is not a way I want it to be.<br class=""><br class="">Here is an example:<br class="">```<br class="">// I have a context<br class="">protocol Context : class {<br class="">&nbsp;/* some */<br class="">}<br class=""><br class="">// I have an object that has sense only in context<br class="">class ObjectInContext {<br class="">&nbsp;private weak var context: Context?<br class=""><br class="">&nbsp;init(context: Context) {<br class="">&nbsp;&nbsp;&nbsp;self.context = context<br class="">&nbsp;}<br class="">}<br class=""><br class="">// This is what I want to do<br class="">// The object graph has a cycle, but there is no a retain cycle<br class="">class ContextA : Context {<br class="">&nbsp;let object: ObjectInContext<br class=""><br class="">&nbsp;init() {<br class="">&nbsp;&nbsp;&nbsp;self.object = ObjectInContext(context: self) // this code will not compile for many good reasons<br class="">&nbsp;}<br class="">}<br class=""><br class="">// This is workaround #1<br class="">// It looks bad for 2 reasons: implicitly unwrapped optional, it is easy to forget to initialize object<br class="">class ContextB : Context {<br class="">&nbsp;var object: ObjectInContext!<br class=""><br class="">&nbsp;init() {<br class="">&nbsp;&nbsp;&nbsp;self.object = ObjectInContext(context: self)<br class="">&nbsp;}<br class="">}<br class=""></div></div></blockquote></div><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">The IUO is the typical pattern here.&nbsp;</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Forgetting to initialize an IUO is less of a problem than it would be in C or ObjC. Access to an IUO is checked at runtime. If you forget to initialize self.object then the process will deliberately halt the first time you try to use it.</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">--&nbsp;</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Greg Parker &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><a href="mailto:gparker@apple.com" class="">gparker@apple.com</a>&nbsp; &nbsp; &nbsp;Runtime Wrangler</div></div></blockquote><br class=""></div><div><div>I agree that IUO is the best solution for now.</div><div>Sometimes I search though Scala to find the best way of doing things. They've came up with lazy as the best practice for such cases.</div><div><br class=""></div><div>I’ve found (SR-1042 Make "lazy var" threadsafe)[<a href="https://bugs.swift.org/browse/SR-1042" class="">https://bugs.swift.org/browse/SR-1042</a>]. Implementing this will help. But I am not sure if it will be implemented any time soon.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Anton Mironov</div></div><br class=""></body></html>