<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 should mention I’m trying this in a Playground.)</div><div class=""><br class=""></div><div class="">I think I tried a similar solution at some point, but it doesn’t seem to work. I get two errors out of that cast:</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><b class="">Playground execution failed: /var/folders/1v/42yjnyj16bb16j19q25hzghr0000gn/T/./lldb/13003/playground346.swift:168:27: error: member 'flatten' cannot be used on value of protocol type 'SequenceType'; use a generic constraint instead</b></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><b class="">let flatArray = (mapArray as SequenceType).flatten() // Error!</b></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><b class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ~~~~~~~~~~^~~~~~~~~~~~~~~~ ~~~~~~~</b></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><b class="">/var/folders/1v/42yjnyj16bb16j19q25hzghr0000gn/T/./lldb/13003/playground346.swift:168:30: error: protocol 'SequenceType' can only be used as a generic constraint because it has Self or associated type requirements</b></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><b class="">let flatArray = (mapArray as SequenceType).flatten() // Error!</b></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><b class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^</b></div></div><div class=""><b class=""><br class=""></b></div><div class="">I can’t seem to find a way to cast it to something that works.</div><div class=""><br class=""></div><div class="">As for the flatMap, I know it’s a valid alternative, but I was trying to make this work just for the understanding you get through exploration of these weird cases.</div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 5, 2015, at 12:43 PM, Harlan Haskins &lt;<a href="mailto:harlan@harlanhaskins.com" class="">harlan@harlanhaskins.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 dir="auto" class=""><div class="">A quick-and-dirty way would be to explicitly cast to the protocol type before calling the function.</div><div class=""><br class=""></div><div class="">let flatArray = (mapArray as SequenceType).flatten()<br class=""></div><div class=""><br class=""></div><div class="">Or in this case you could just mapArray.flatMap()</div><div class=""><br class="">On Dec 5, 2015, at 12:38 PM, Vinicius Vendramini &lt;<a href="mailto:vinivendra@gmail.com" class="">vinivendra@gmail.com</a>&gt; wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div class="">So I was playing around with Sequences, and I came across a problem I haven't been able to solve in a decent way. I’ve reduced it to a simpler example here:</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> array : [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>] = [<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span>]</div><div style="margin: 0px; line-height: normal; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> mapArray = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">array</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span> { $0 }</div><div style="margin: 0px; line-height: normal; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> flatArray = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">mapArray</span>.flatten() <span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// Error!</span></div></div></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">This last line of code prints the following error:</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><b class="">let flatArray = mapArray.flatten() // Error!</b></div><div style="margin: 0px; line-height: normal;" class=""><b class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^~~~~~~~</b></div><div style="margin: 0px; line-height: normal;" class=""><b class="">Swift.SequenceType:4:17: note: found this candidate</b></div><div style="margin: 0px; line-height: normal;" class=""><b class="">&nbsp; &nbsp; public func flatten() -&gt; FlattenSequence&lt;Self&gt;</b></div><div style="margin: 0px; line-height: normal;" class=""><b class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^</b></div><div style="margin: 0px; line-height: normal;" class=""><b class="">Swift.CollectionType:4:17: note: found this candidate</b></div><div style="margin: 0px; line-height: normal;" class=""><b class="">&nbsp; &nbsp; public func flatten() -&gt; FlattenCollection&lt;Self&gt;</b></div><div style="margin: 0px; line-height: normal;" class=""><b class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^</b></div><div style="margin: 0px; line-height: normal;" class=""><b class="">Swift.CollectionType:4:17: note: found this candidate</b></div><div style="margin: 0px; line-height: normal;" class=""><b class="">&nbsp; &nbsp; public func flatten() -&gt; FlattenBidirectionalCollection&lt;Self&gt;</b></div><div style="margin: 0px; line-height: normal;" class=""><b class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^</b></div></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">As far as I understand it, the error happens because several protocols extensions implement the `flatten` method, and `mapArray` (which is of type `Array&lt;Optional&lt;Int&gt;&gt;`) conforms to a few of those, which means the compiler has no way of knowing which one I intend on using.</div><div class=""><br class=""></div><div class="">How do I solve this?</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=z0Ft8gvIaLt1zLwfkl4og6osLD9N8tlWJY1PY2zqWLXW6AUCPnOB1o9xfIVkBT7EkA9CEGSydcVMc1v4TL6nB0nE21DWkYMptNl-2F7jZLzzithZa7013s-2BChRCoJM-2BGk9OsnvmRaC30FTaE-2FVlHkvMUsvjdoeauxwvpsw5AzwcQe5GdMEuAtJvIppCfGmkNH-2BkV94ltvCGsiSnEht9rIX4gfedd27iMQvpZUmowhXKwQ-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">

</div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-users mailing list</span><br class=""><span class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br class=""></div></blockquote></div></div></blockquote></div><br class=""></body></html>