[swift-evolution] Enhanced existential types proposal discussion

L Mihalkovic laurent.mihalkovic at gmail.com
Mon May 30 10:41:02 CDT 2016


> On May 30, 2016, at 5:13 AM, Matthew Johnson via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
> 
> Sent from my iPad
> 
> On May 29, 2016, at 9:43 PM, Charles Srstka <cocoadev at charlessoft.com <mailto:cocoadev at charlessoft.com>> wrote:
> 
>>> On May 29, 2016, at 9:20 PM, Matthew Johnson <matthew at anandabits.com <mailto:matthew at anandabits.com>> wrote:
>>> 
>>> On May 29, 2016, at 5:43 PM, Charles Srstka via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>>>> On May 29, 2016, at 5:16 PM, Austin Zheng <austinzheng at gmail.com <mailto:austinzheng at gmail.com>> wrote:
>>>>> 
>>>>> I think the problem here is that P == P is true, but P : P is not (a protocol does not conform to itself).
>>>> 
>>>> But if you have a variable, parameter, etc. typed as P, that’s *not* the protocol, since protocols aren’t concrete entities. What you have there, by definition, is something that conforms to P. Similarly, something like [P] is just a collection of things, perhaps of various types, which all have the common feature that they conform to P.
>>> 
>>> You have an existential value of type P.  It is a well known frustration in Swift that the existential type corresponding to a protocol does not conform to the protocol.  This has been discussed off and on at different times.  
>>> 
>>> There are a couple of reasons this is the case.  IIRC in some cases it actually isn't possible for the existential to conform to the protocol in a sound way.  And even when it is possible, I believe it has been said that it is more difficult to implement than you might think.  Hopefully the situation will improve in the future but I'm not aware of any specific plans at the moment.
>> 
>> It’s been my understanding that a variable typed P in swift is equivalent to what we would have called id <P> in Objective-C—that is, an object of unknown type that conforms to P. Is this not the case? I am curious what the conceptual difference would be, as well as the rationale behind it.
> 
> Existentials have their own type in Swift.  The problem you are running into is because the generic constraint is looking at the existential type of P and asking if that type conforms to P (which it does not - you can't write the conformance and the compiler does not provide it for you).  It is not asking if the type of the object underlying the existential value conforms to P (which it necessarily does).  When you have a value of type P you have already erased the type of the underlying object.

I'd say it is the opposite: there is no really erasure per-se but more masking. Depending on the kind of existential, there are 3 possible types of wrappers used by the compiler. If there was real erasure, you’d never be able to do a “let x = ar[0] as? S because the S-ness would be lost. In this situation the existential needs to be opened to perform a checked cast.


> 
>> 
>> Charles
>> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160530/d1f8a1da/attachment.html>


More information about the swift-evolution mailing list