<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=""><div class="">Longstanding bug, <a href="rdar://problem/17277899" class="">rdar://problem/17277899</a>. Surprisingly few people have asked for it.</div><div class=""><br class=""></div><div class="">Jordan</div><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 3, 2016, at 23:29, Karl via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Currently it’s not possible to have an unowned optional value. E.g:<br class=""><br class="">class A {<br class=""> unowned var parent : A? // ‘unowned’ may only be applied to class and class-bound protocol types, not ‘A?'<br class=""> deinit { <br class=""> if let p = parent { print("Bye, mom!") }<br class=""> print("deallocating")<br class=""> }<br class="">}<br class=""><br class="">Sometimes you want an unowned value, but it doesn’t actually need to be set.<br class=""><br class="">In a specific case I was working on, we wanted to use a private embedded struct to wrap some functionality away as a unit, but we need a reference back to the enclosing instance. We could have a weak reference, but that comes with additional overheads, and it’s unnecessary because that struct code will all get inlined and the reference will just be a pointer back to ‘self’ (it would be cool if the compiler could detect that and just not emit the variable at all, but I don’t think it’ll do that). In any case, in order to initialise it with the parent that reference needs to be an optional/implicitly-unwrapped-optional (so that it can be initialised to nil and then set after the parent calls super.init).<br class=""><br class="">Given that weak optionals work, I’m tempted to think it’s a bug, but I’m asking in case it was intentionally omitted.<br class=""><br class=""><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></div></blockquote></div><br class=""></body></html>