<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="">The problem is that an array of protocols is stored using a different representation from an array of plain structs (or classes, for that matter). Converting from [Float] to [MyProtocol] is therefore an O(N) operation, while a normal upcast between class types is O(1) (by sharing the underlying storage).</div><div class=""><br class=""></div><div class="">This does keep tripping people up, though, so maybe an explicit "as" should be considered good enough. The typical workarounds are either to use "as [MyProtocol]" to begin with (as Trent mentioned) or to use 'map' to build a new array ("return a.map { $0 }").</div><div class=""><br class=""></div><div class="">(Any change in this space would need to go through the&nbsp;<a href="https://github.com/apple/swift-evolution/blob/master/process.md" class="">Swift Evolution Process</a>.)</div><div class=""><br class=""></div><div class="">Best,</div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 25, 2016, at 19:58, Ryan Walklin via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Austin,&nbsp;<div class=""><br class=""></div><div class="">Thanks for your reply. Sorry you’re right, my struct types don’t work if they are in in existing array of type [MyType] as below.</div><div class=""><br class=""></div><div class="">I’ve been trying to work round it and found that with the following code:&nbsp;</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="">extension MyType: 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="">var array1: [MyType]</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="">fun foo() -&gt; [MyProtocol] {</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class="">&nbsp; &nbsp; return array1</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class="">}</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="">func foo2 -&gt; [MyProtocol] {</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class="">&nbsp; &nbsp; return [myType1, myType2]</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class="">}</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">The foo pattern fails, but the foo2 pattern works. My existing code with struct type was all of type foo2, so I didn’t appreciate the error. As you stay, still seems like a problem with type inference.</div><div class=""><br class=""></div><div class="">Ryan</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 26 Jan 2016, at 2:47 PM, Austin Zheng &lt;<a href="mailto:austinzheng@gmail.com" class="">austinzheng@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" 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="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; 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="">struct</span> Blah : <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; 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() -&gt; [<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="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> a = [<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Blah</span>(), <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Blah</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="">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="">&nbsp; </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="">On Jan 25, 2016, at 7:36 PM, Ryan Walklin via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" 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.&nbsp;</div><div class=""><br class=""></div><div class="">ie.&nbsp;</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>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></div>_______________________________________________<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></blockquote></div><br class=""></body></html>