<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></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 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=""><div class="">The IUO is the typical pattern here.&nbsp;</div><div class=""><br class=""></div><div class="">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=""><br class=""></div><div class=""><br class=""></div><div class="">--&nbsp;</div><div class="">Greg Parker &nbsp; &nbsp; <a href="mailto:gparker@apple.com" class="">gparker@apple.com</a>&nbsp; &nbsp; &nbsp;Runtime Wrangler</div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>