[swift-evolution] [Pre-proposal] Replace [Foo] With CollectionType
Charles Srstka
cocoadev at charlessoft.com
Fri May 27 15:36:09 CDT 2016
> On May 27, 2016, at 1:53 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>
> Firstly, the syntax is about to get a lot cleaner. Soon, your example will be:
>
> func doSomething<
> S: Sequence, T: Sequence, U: Sequence
> >(foos: S, bars: T, bazzes: U)
> where S.Element == Foo, T.Element == Bar, U.Element == Baz
I wonder if it would be possible to introduce angle brackets on protocols to enumerate associated-type requirements? AFAIK, currently the angle brackets are not allowed at all, so it would be purely additive. Then, you could do something like:
func doSomething(foos: Sequence<Element == Foo>, bars: Sequence<Element == Bar>, bazzes: Sequence<Element == Baz>)
I don’t know about you, but that looks so much cleaner, for a function that doesn’t itself need to be generic.
(The generalized existentials solution is good, too. However, it requires making a separate typealias for every protocol you want to do this with, whereas the above idea wouldn’t.)
> Second, this syntax shows necessary complexity. Case in point: an array is a Collection with randomly accessible elements, yet in your example you chose SequenceType (aka Sequence). An instance of a type conforming to Sequence may or may not have randomly accessible elements, it may or may not be consumed after one pass, it may or may not have copy-on-write behavior, and it may or may not be laid out contiguously in memory (which is not guaranteed for all arrays but is for arrays with elements of primitive type)--and that's if it's entirely held in memory at all.
>
> By writing out the function declaration the way it's shown above, it's clear what considerations that function will have to take into account. Assuming that it can operate on arbitrary sequences just like it can an array is a recipe for a lot of pain.
Yeah, I defaulted to Sequence just because I’ve been writing stuff lately that just iterates through a collection that’s passed in. If what the OP is proposing came to pass, it would need to map to Collection instead. Of course, sometimes all you need is a Sequence, so your point is well-taken. We do need *some* way to simplify this syntax, though.
Charles
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160527/0b3b9c1a/attachment.html>
More information about the swift-evolution
mailing list