<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="">Hi Austin, <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: </div><div class=""><br class=""></div><div class=""><p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class="">extension MyType: MyProtocol {}</p>
<p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16); min-height: 16px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class="">var array1: [MyType]</p>
<p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16); min-height: 16px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class="">fun foo() -> [MyProtocol] {</p>
<p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class=""> return array1</p>
<p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class="">}</p>
<p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16); min-height: 16px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class="">func foo2 -> [MyProtocol] {</p>
<p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class=""> return [myType1, myType2]</p>
<p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Courier; color: rgb(16, 16, 16);" class="">}</p></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><blockquote type="cite" class=""><div class="">On 26 Jan 2016, at 2:47 PM, Austin Zheng <<a href="mailto:austinzheng@gmail.com" class="">austinzheng@gmail.com</a>> 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() -> [<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: #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=""> </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 <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> 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. </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>_______________________________________________<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></body></html>