<div dir="ltr">Better support for existentials (see the generics manifesto, <a href="https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md">https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md</a>) should obviate the need for any sort of sugar or compiler magic to do this kind of thing.<div><br></div><div>typealias AnyCollection&lt;T&gt; = Any&lt;Collection where .Element == T, ...&gt;</div><div>func doSomething(collection: AnyCollection&lt;Foo&gt;)<br><div><br></div><div>Austin</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, May 24, 2016 at 10:57 AM, Haravikk via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</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">One thing that I see a lot in code, and sometimes have to stop myself from doing, is using shorthand array types, such as [Foo], in function declarations where CollectionType could just as easily be used. For example, the following two declarations can take collections of values, but the first will only take them in the form of an Array:<div><br></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>func doSomething(values:[Foo]) { … }</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>func doSomething&lt;C:CollectionType where C.Generator.Element:Foo&gt;(values:C) { … }</font></div><div><br></div><div>The latter form is something that new users of Swift tend not to know they can do, and which even experienced Swift developers may not use for the sake of brevity, but it can come up quite a lot. What I’d like to propose is that [Foo], when used in a function, should produce the latter form behind the scenes, requiring the developer to specify Array&lt;Foo&gt; if they actually need it to be an Array for some reason. Though this would become inconsistent with variables/properties which would still have to be Array&lt;Foo&gt; since a type is required.</div><div><br></div><div><br></div><div>An alternative would be if we could specify protocol generics in a more succinct form, for example:</div><div><br></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>func doSomething(values:Collection&lt;Foo&gt;) { … }</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>func doSomething(values:Sequence&lt;Foo&gt;) { … } // Many array functions are linear anyway so could just as easily take sequences</font></div><div><font face="Monaco"><br></font></div>Note: This would not be the same as type-erased wrappers such as AnySequence&lt;Foo&gt;, but rather a shorthand for &quot;Sequence where Generator.Element:Foo&quot;<div><br></div><div><br></div><div><br><div>In essence I’m hoping to discuss whether we should try to remove the temptation to limit functions to arrays only or not, or if there are other ways to encourage more use of sequence and collection for flexibility; I try wherever possible to have my methods take sequences if they can, and only take collections if they need to (and never arrays only), but I can understand why not everyone does this, as it’s not the friendliest thing to add and declaring [Foo] looks so much neater.</div></div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>