<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="">I'm reasonably sure Arrays and other collections are covariant, but any user-defined generic type is invariant. This came up in the discussion about making all generics covariant by default.</div><div class=""><br class=""></div><div class="">For some reason this works for me:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span> MyProtocol {}</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Float</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">: </span>MyProtocol<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> foo() -> [<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyProtocol</span>] {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> a = [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Float</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>), <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Float</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>)] <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span> [<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyProtocol</span>]</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>return<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> a</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">but this doesn't (with or without the cast):</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> foo() -> [<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyProtocol</span>] {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> a = [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Float</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>), <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Float</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>)] <span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">//as [Float]</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> a </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> [</span>MyProtocol<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">]</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">Nor does this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> foo() -> [<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyProtocol</span>] {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> a = [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Float</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>), <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Float</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>)] <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span> [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Float</span>]</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> b = a <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span> [<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyProtocol</span>]</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>return<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> b</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">This definitely looks like a bug, or at least something that needs a better compiler error message. (I'm running this in an OS X Xcode project with Swift 2.1, but it doesn't work with Swift 2.2 either.)</div><div class=""><br class=""></div><div class="">Best,</div><div class="">Austin</div><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 25, 2016, at 7:51 PM, Trent Nadeau <<a href="mailto:tanadeau@gmail.com" class="">tanadeau@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I think arrays along with other generics are invariant.</div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Jan 25, 2016 at 10:50 PM, Trent Nadeau <span dir="ltr" class=""><<a href="mailto:tanadeau@gmail.com" target="_blank" class="">tanadeau@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="">That code works fine for me if I change the cast to "as [MyProtocol]".<div class=""><br class=""></div><div class=""><span class=""><div class="">protocol MyProtocol {}</div><div class=""><br class=""></div><div class="">struct Blah : MyProtocol { }</div><div class=""><br class=""></div><div class="">func foo() -> [MyProtocol] {</div></span><div class=""> let a = [Blah(), Blah()] as [MyProtocol]</div><div class=""> return a</div><div class="">}</div></div></div><div class="gmail_extra"><div class=""><div class="h5"><br class=""><div class="gmail_quote">On Mon, Jan 25, 2016 at 10:47 PM, Austin Zheng via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">Hi Ryan,<div class=""><br class=""></div><div class="">Are you sure it works with user-defined struct types? I tried the following code, and got the same error:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">protocol</span> MyProtocol {}</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px" class=""><br class=""></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">struct</span> Blah : <span style="color:#4f8187" class="">MyProtocol</span> { }</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px" class=""><br class=""></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">func</span> foo() -> [<span style="color:#4f8187" class="">MyProtocol</span>] {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo" class=""> <span style="color:#bb2ca2" class="">let</span> a = [<span style="color:#4f8187" class="">Blah</span>(), <span style="color:#4f8187" class="">Blah</span>()] <span style="color:#bb2ca2" class="">as</span> [<span style="color:#4f8187" class="">Blah</span>]</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)" class=""><span style="" class=""> </span>return<span style="" class=""> a</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo" class="">}</div></div><div class=""><br class=""></div><div class="">'class Blah' works fine.</div><div class=""><br class=""></div><div class="">Even so, this is not the behavior I would have expected. Perhaps someone else can explain why [T] can't be upcasted to [P] when T is a struct and T : P.</div><div class=""><br class=""></div><div class="">Best,</div><div class="">Austin</div><div class=""><br class=""></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class=""><div class=""><div class="">On Jan 25, 2016, at 7:36 PM, Ryan Walklin via swift-users <<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>> wrote:</div><br class=""></div></div><div class=""><div class=""><div class=""><div style="word-wrap:break-word" class="">Hi All,<div class=""><br class=""></div><div class="">I have a function which returns a closure, which then returns an array of objects conforming to a protocol. </div><div class=""><br class=""></div><div class="">ie. </div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:13px;line-height:normal;font-family:Courier;color:rgb(16,16,16)" class="">protocol MyProtocol {}</div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Courier;color:rgb(16,16,16);min-height:16px" class=""><br class=""></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Courier;color:rgb(16,16,16)" class="">extension Float: MyProtocol {}</div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Courier;color:rgb(16,16,16)" class="">extension OtherType: MyProtocol {}</div></div><div class=""><br class=""></div><div class="">When I try to return an array of Floats, I get the error “Cannot convert value of type '[Float]' to closure result type ‘[MyProtocol]”.</div><div class=""><br class=""></div><div class="">Other struct-based types work fine.</div><div class=""><br class=""></div><div class="">Is this not possible with standard library types?</div><div class=""><br class=""></div><div class="">Regards,</div><div class=""><br class=""></div><div class="">Ryan</div></div></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div><br class="">_______________________________________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""><br clear="all" class=""><div class=""><br class=""></div></div></div><span class="HOEnZb"><font color="#888888" class="">-- <br class=""><div class="">Trent Nadeau</div>
</font></span></div>
</blockquote></div><br class=""><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature">Trent Nadeau</div>
</div>
</div></blockquote></div><br class=""></body></html>