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

Haravikk swift-evolution at haravikk.me
Sat May 28 15:08:33 CDT 2016


> On 28 May 2016, at 20:16, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> 
> I couldn't disagree more. Generic programming is a far more advanced concept than arrays and dictionaries, and learners should be able to use them without contending with angle brackets they don't understand.
> 
> Fundamentally, even for an experienced user of the language, working with Collections and Sequences requires more consideration than working with arrays. It's not just a matter of how these things are spelled. It seems like you want to punish users who don't have the time or inclination to genericize their algorithms by making them write more, in the hopes that if you make working with arrays difficult enough, people will switch to generic containers. That's bonkers.

It’s not about making working with arrays difficult, but if we can make working with basic generic collections/sequences just as easy, then it makes sense to encourage their use as much as possible. Like in the example below, Sequence.of<Foo> is pretty straightforward, and far more flexible and useful than just using [Foo], as you can trivially change the type of collection you use later.

More complex generics definitely need to be explored separately, but Sequence.of<Foo> should be just as simple to use as [Foo], it’s just a little longer because of the ability to distinguish between sequence and collection.

> On Sat, May 28, 2016 at 14:38 Haravikk via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> 
>> On 27 May 2016, at 15:31, plx via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>>   protocol Sequence {
> 
>>     typealias of<E> == S: Self where S.Iterator.Element == E
>>   }
>> 
> 
>>   // sequence-accepting variant
>>   func doSomething<S:Sequence.of<Foo>>(values: S) { … }
> 
> 
> This is a nice alternative; would it actually need to be declared in the angle brackets? Could it be done as:
> 
> 	func doSomething(values:Sequence.of<Foo>) { … }
> 
> As long as it would work the same as a generic declaration this could be a good way to do it, either that or a SequenceOf type alias as mentioned.
> 
> 
> Still, I kind of feel like we need to do something with the array type shorthand, but I wonder if perhaps we could just get rid of it altogether, to prevent its use entirely? i.e- all instances of [Foo] can be replaced with Array<Foo>, but we would encourage the use of Sequence.of/SequenceOf/Collection.of/CollectionOf first wherever possible.
> 
> As more types become available that are ArrayLiteralConvertible it seems like we should discourage restriction of a parameter to Array except when a developer explicitly chooses it. This problem will come up with the Dictionary type shorthand as well if Swift gets some kind of Map protocol to abstract it, and we don’t even have a Set-specific syntax so it seems like it may be fairer to remove these shorthands.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160528/5e0c6dbe/attachment.html>


More information about the swift-evolution mailing list