<div dir="ltr">That code works fine for me if I change the cast to "as [MyProtocol]".<div><br></div><div><div>protocol MyProtocol {}</div><div><br></div><div>struct Blah : MyProtocol { }</div><div><br></div><div>func foo() -> [MyProtocol] {</div><div> let a = [Blah(), Blah()] as [MyProtocol]</div><div> return a</div><div>}</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 25, 2016 at 10:47 PM, Austin Zheng via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> 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">Hi Ryan,<div><br></div><div>Are you sure it works with user-defined struct types? I tried the following code, and got the same error:</div><div><br></div><div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">protocol</span> MyProtocol {}</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px"><br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">struct</span> Blah : <span style="color:#4f8187">MyProtocol</span> { }</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;min-height:16px"><br></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">func</span> foo() -> [<span style="color:#4f8187">MyProtocol</span>] {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">let</span> a = [<span style="color:#4f8187">Blah</span>(), <span style="color:#4f8187">Blah</span>()] <span style="color:#bb2ca2">as</span> [<span style="color:#4f8187">Blah</span>]</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:#000000"> </span>return<span style="color:#000000"> a</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo">}</div></div><div><br></div><div>'class Blah' works fine.</div><div><br></div><div>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><br></div><div>Best,</div><div>Austin</div><div><br></div><div><br><div><blockquote type="cite"><div><div class="h5"><div>On Jan 25, 2016, at 7:36 PM, Ryan Walklin via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br></div></div><div><div><div class="h5"><div style="word-wrap:break-word">Hi All,<div><br></div><div>I have a function which returns a closure, which then returns an array of objects conforming to a protocol. </div><div><br></div><div>ie. </div><div><br></div><div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Courier;color:rgb(16,16,16)">protocol MyProtocol {}</div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Courier;color:rgb(16,16,16);min-height:16px"><br></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Courier;color:rgb(16,16,16)">extension Float: MyProtocol {}</div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Courier;color:rgb(16,16,16)">extension OtherType: MyProtocol {}</div></div><div><br></div><div>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><br></div><div>Other struct-based types work fine.</div><div><br></div><div>Is this not possible with standard library types?</div><div><br></div><div>Regards,</div><div><br></div><div>Ryan</div></div></div></div>_______________________________________________<br>swift-users mailing list<br><a href="mailto: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/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div><br>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Trent Nadeau</div>
</div>