@Karl,<div><br></div><div>You say "<font size="2"><span style="background-color:rgba(255,255,255,0)">In the second example, you’re creating WeakReference<P>. P does not conform to P or to AnyObject.", but P does conform to AnyObject.</span></font></div><div><font size="2"><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.301961);"><br></span></font></div><div><font size="2"><span style="background-color:rgba(255,255,255,0)"></span><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.301961);">I suspect it is a compiler limitation/ bug. </span></font></div><div><font size="2"><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.301961);"><br></span></font></div><div><font size="2"><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.301961);"> -- Howard. <br></span></font><br>On Thursday, 1 September 2016, Karl <<a href="mailto:razielim@gmail.com">razielim@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On 1 Sep 2016, at 03:23, Howard Lovatt via swift-users <<a href="javascript:_e(%7B%7D,'cvml','swift-users@swift.org');" target="_blank">swift-users@swift.org</a>> wrote:</div><br><div><div dir="ltr">Playing around I found that if you make the protocol @objc instead of AnyObject then it works :). EG:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>struct WeakReference<T: AnyObject> {</div><div> weak var value: T?</div><div>}</div><div>@objc protocol P { // Note @objc, class or AnyObject does not work</div><div> var i: Int { get }</div><div>}</div><div>class CP: P {</div><div> var i: Int = 0</div><div>}</div><div>let weakPs: [WeakReference<P>] = [WeakReference(value: cP)] // Note typed as `[WeakReference<P>]`</div><div>print("P: \(weakPs[0].value!.i)") // 0</div></blockquote><div><p><span></span>Not a 'pure' Swift solution :(, but OK in my case.</p></div></div><div class="gmail_extra"><br clear="all"><div><div data-smartmail="gmail_signature"> -- Howard.<br></div></div>
<br><div class="gmail_quote">On 29 August 2016 at 16:21, Howard Lovatt <span dir="ltr"><<a href="javascript:_e(%7B%7D,'cvml','howard.lovatt@gmail.com');" target="_blank">howard.lovatt@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>I am wanting to use weak references in generic data structures; in the example below Array, but in general any generic type. I can almost get it to work :(</div><div><br></div><div>My experiments started off well; the following works:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>// Array of weak references OK</div><div>struct WeakReference<T: AnyObject> {</div><div> weak var value: T?</div><div>}</div><div>class C {</div><div> var i: Int = 0</div><div>}</div><div>let c = C() // Strong reference to prevent collection</div><div>let weakCs = [WeakReference(value: c)] // OK</div><div>print("C: \(weakCs[0].value!.i)") // 0</div></blockquote><br>I can add a protocol:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>// Array of weak references that implements a protocol OK</div><div>protocol P: AnyObject { // Note AnyObject</div><div> var i: Int { get }</div><div>}</div><div>class CP: P {</div><div> var i: Int = 0</div><div>}</div><div>let cP = CP() // Strong reference to prevent collection</div><div>let weakCPs = [WeakReference(value: cP)] // OK</div><div>print("CP: \(weakCPs[0].value!.i)") // 0</div></blockquote><div><div><br></div><div>But when I want an array of weak references to the protocol I get an error:</div><div><br></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>// Array of weak references of a protocol not OK</div><div>let weakPs: [WeakReference<P>] = [WeakReference(value: cP)] // Using 'P' as a concrete type conforming to protocol 'AnyObject' is not supported</div><div>print("P: \(weakPs[0].value!.i)") // 0</div></blockquote><div><div><br></div><div>Is there something I have missed?</div><div><br></div><div>The error message, "Using 'P' as a concrete type conforming to protocol 'AnyObject' is not supported", implies that it is a temporary limitation of the compiler; is this going to be fixed? Should I lodge a bug report?</div><div><br></div><div>Thanks in advance for any advice,</div><div><br clear="all"><div><div data-smartmail="gmail_signature"> -- Howard.<br></div></div>
</div></div></div>
</blockquote></div><br></div>
______________________________<wbr>_________________<br>swift-users mailing list<br><a href="javascript:_e(%7B%7D,'cvml','swift-users@swift.org');" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br><div>Your problem is protocol self-conformance. In the first example, you’re creating WeakReference<CP>. CP conforms to P and to AnyObject. In the second example, you’re creating WeakReference<P>. P does not conform to P or to AnyObject.</div><div><br></div><div>As for why @objc fixes it? … ¯\_(ツ)_/¯ all bets are off whenever @objc gets involved in anything.</div><div><br></div><div>Karl</div></div></blockquote></div><br><br>-- <br>-- Howard.<br>