[swift-evolution] Question about heterogeneous collections

Jason Sadler jason.sadler at gmail.com
Fri Apr 1 14:00:20 CDT 2016


Ugh, sorry to spam the list like this (and also sorry I broke the threading…)
Dave’s hint to use “when Element == MyProtocol” (https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160328/013922.html) worked great for the example that I posted but falls down in some other cases. I count three possibilities:

0. (original case…now works) My array is [MyProtocol]
1.  If my array is [SimpleStruct] (where SimpleStruct : MyProtocol) I get “‘MyProtocol’ is not convertible to ‘SimpleStruct’”
2.  If my array is [SubProtocol] (where SubProtocol : MyProtocol) I get “Type ‘MyProtocol’ does not conform to protocol ‘SubProtocol’”

I can work around #1 by duplicating my extension to CollectionType for both Element : MyProtocol *and* Element == MyProtocol, but that’s less than ideal. And I’m not sure if solving #2 is possible in this situation. (Unfortunately for me, #2 is actually what I’m trying to do in my app.)


> On Apr 1, 2016, at 2:38 PM, Jason Sadler <jason.sadler at gmail.com> wrote:
> 
> I should double check before I post. That Stack Overflow question has the same answer Dave posted earlier in this thread, which is more correct - it just has fewer votes, and isn’t marked as the accepted answer. Sorry!
> 
>> On Apr 1, 2016, at 2:32 PM, Jason Sadler <jason.sadler at gmail.com> wrote:
>> 
>> Thanks everyone for your answers - this has helped p a lot. However because I’m not a language design pro, I’m a little unclear still on what the difference is between `Element == MyProtocol` and `Element: MyProtocol`. Obviously the latter is “Element conforms to MyProtocol” but I don’t know how to make an English sentence out of the former.
>> 
>> I re-read some of the Generics chapter in the Swift book this morning but I wasn’t able to solidify my model of these two concepts.  Based on the lack of information when searching on Stack Overflow (the most helpful thing I’ve found was this, which suggests putting things in a box: http://stackoverflow.com/questions/33112559/protocol-doesnt-conform-to-itself/33524927#33524927) and asking on swift-users, could I suggest that this could use some more explicit documentation somewhere?
>> 
>> Thanks again,
>> Jason
> 



More information about the swift-evolution mailing list