[swift-evolution] [Pre-proposal] Replace [Foo] With CollectionType

Matthew Johnson matthew at anandabits.com
Sat May 28 14:23:10 CDT 2016



Sent from my iPad

> On May 28, 2016, at 12:40 PM, Thorsten Seitz <tseitz42 at icloud.com> wrote:
> 
> 
>>> Am 27.05.2016 um 20:47 schrieb Matthew Johnson via swift-evolution <swift-evolution at swift.org>:
>>> 
>>> 
>>>> On May 27, 2016, at 12:05 PM, Charles Srstka via swift-evolution <swift-evolution at swift.org> wrote:
>>>> 
>>>> On May 27, 2016, at 9:31 AM, plx via swift-evolution <swift-evolution at swift.org> wrote:
>>>> 
>>>> For the Sequence/Collection it’s a lot of work for IMHO a rather minor convenience, but for more-complex type associated-type relationships it could start to pay its own way.
>>> 
>>> Is it really that minor, though? For something so commonly encountered as methods that take sequences/collections, this:
>>> 
>>> func doSomething(foos: [Foo], bars: [Bar], bazzes: [Baz])
>>> 
>>> is not only a whole lot easier to type, but is worlds clearer to read than:
>>> 
>>> func doSomething<S: SequenceType, T: SequenceType, U: SequenceType where S.Generator.Element == Foo, T.Generator.Element == Bar, U.Generator.Element == Baz>(foos: S, bars: T, bazzes: U)
>>> 
>>> Not only is the latter line intimidating to look at as is, but it separates the contained type from the parameters themselves. Given how unwieldy this second form is, it seems almost certain that the former line will be used more frequently in the real world.
>> 
>> When generalized existentials are introduced (Austin Zheng has a proposal for this) you will be able to do this (assuming we switch to the `&` syntax:
>> 
>> typealias SequenceOf<T> = Sequence where .Element == T
>> 
>> func doSomething(foos: SequenceOf<Foo>, bars: SequenceOf<Bar>, bazzes: SequenceOf<Baz>)
> 
> That’s a really nice solution, which gets even nicer with plx suggestion of putting the typealias within the protocol like this: 
> 
> protocol Sequence {
>     typealias of<E> = S: Self where .Element == E
> }

That is very nice!

> 
> -Thorsten
> 
> 
> 
>> 
>> It’s still slightly more verbose than the array shorthand, but it’s a far cry from what you have to do with generics today.
>> 
>> If you wanted it to be generic you could write it as:
>> 
>> func doSomething<S: SequenceOf<Foo>, T: SequenceOf<Bar>, SequenceOf<Baz>(foos: S, bars: T, bazzes: U)
>> 
>>> 
>>> Charles
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160528/612d8202/attachment.html>


More information about the swift-evolution mailing list