[swift-evolution] [swift-evolution-announce] [Review] SE-0089: Replace protocol<P1, P2> syntax with Any<P1, P2>

Dave Abrahams dabrahams at apple.com
Wed Jun 8 16:49:53 CDT 2016


on Wed Jun 08 2016, Jordan Rose <jordan_rose-AT-apple.com> wrote:

>> On Jun 8, 2016, at 13:16, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> 
>> on Wed Jun 08 2016, Thorsten Seitz <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>
>>> Ah, thanks, I forgot!  I still consider this a bug, though (will have
>>> to read up again what the reasons are for that behavior).
>> 
>> Yes, but in the case of the issue we're discussing, the choices are:
>> 
>> 1. Omit from the existential's API any protocol requirements that depend
>>   on Self or associated types, in which case it *can't* conform to
>>   itself because it doesn't fulfill the requirements.
>> 
>> 2. Erase type relationships and trap at runtime when they don't line up.
>> 
>> Matthew has been arguing against #2, but you can't “fix the bug” without
>> it.
>
> #1 has been my preference for a while as well, at least as a starting
> point. 

But as I've been suggesting, #1 may not worth generalizing existentials
for, especially considering that Doug says by a factor of 10x people who
want parameterized protocols are asking for things like Collection<T>.

> It's possible we could also "open" the existential when it's only used
> by one parameter, i.e. the first would be legal and the second
> wouldn't:
>
> func foo<X: Hashable>(x: X) { … }
> func test(x: Any<Hashable>) {
>   foo(x) // okay, passes the dynamic type
> }
>
> func bar<X: Hashable>(a: X, b: X) { … }
> func test(x: Any<Hashable>, y: Any<Hashable>) {
>   bar(x, y) // illegal because x.dynamicType may be different from y.dynamicType
> }
>
> (The check is not as simple as "the generic parameter is only
> mentioned once", because of constraints and such. But you get the
> idea.)

Interesting from a “completing the type system” perspective but perhaps
not very useful.  What are the use cases for this?  What you can do
with Hashable is intimately dependent on the type relationship.

We should really stop using protocols refining Equatable as an exemplar,
because that's a special case that has a special answer when the types
don't match up**. It's not representative of the general case, though.

** and any generalized Existential system we implement ought to support
   that answer!

-- 
Dave


More information about the swift-evolution mailing list