<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 <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> 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=""> /* some */<br class="">}<br class=""><br class="">// I have an object that has sense only in context<br class="">class ObjectInContext {<br class=""> private weak var context: Context?<br class=""><br class=""> init(context: Context) {<br class=""> self.context = context<br class=""> }<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=""> let object: ObjectInContext<br class=""><br class=""> init() {<br class=""> self.object = ObjectInContext(context: self) // this code will not compile for many good reasons<br class=""> }<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=""> var object: ObjectInContext!<br class=""><br class=""> init() {<br class=""> self.object = ObjectInContext(context: self)<br class=""> }<br class="">}<br class=""></div></div></blockquote></div><br class=""><div class="">The IUO is the typical pattern here. </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="">-- </div><div class="">Greg Parker <a href="mailto:gparker@apple.com" class="">gparker@apple.com</a> Runtime Wrangler</div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>