[swift-evolution] [Proposal] Separate protocols and interfaces

Matthew Johnson matthew at anandabits.com
Mon Jan 4 08:24:53 CST 2016


> On Jan 3, 2016, at 10:44 PM, Matthew Johnson <matthew at anandabits.com> wrote:
> 
> 
>> On Jan 3, 2016, at 9:14 PM, Drew Crawford <drew at sealedabstract.com <mailto:drew at sealedabstract.com>> wrote:
>> 
>> Sure, here's the start of the thread: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001856.html <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001856.html>
> Thanks.  Joe was basically saying is that associated types would be automatically bound to the existential for their constraints, or Any if there are no constraints.  
> 
> He didn’t specifically mention anything about Self, but I suppose Self requirements could also be automatically bound to Any if the existential type doesn’t specify anything more specific, although I’m not sure I would like that behavior.
> 
> Self is what would apply in the case of:
> 
>> func compareTwo(first: Comparable, _ second: Comparable) -> Int {  // error!
>>   if first < second {
>>     return -1
>>   }
>>   //...
>> }
> If Self were automatically bound to Any what would this do?  Would it compile and invoke a `<` operator that takes two Any parameters?  That doesn’t seem to make sense to me.  It certainly wouldn’t guarantee you get the correct behavior if first and second were both Int for example.

I gave this some further thought last night and realized what would happen here is pretty clear.  I hadn’t considered existentials where associated types aren’t bound to concrete types before so it just took a few minutes to work through.

Existentials reference a witness table pointing to an actual implementations of the protocol requirements.  Actual implementations require parameters of concrete types.  This means that you must know what that concrete type is and supply a value of that type in order to actually call the member.  The implication of this is that members which require parameters of an associated type that is not bound to a concrete type will not be available on that existential.  

In this example, `<` requires two arguments of type Self.  However, the `Comparable` existential, if allowed, would have Self bound to `Any`, not a concrete type.  Therefore `<` would not be available and you would receive a compiler error on that line.  It would be different than the current error and on a different line of code but it would still fail to compile.

With return types, you do not necessarily need to know the concrete type returned by the implementation.  Swift could theoretically box the result itself into an existential and return that to the caller.  I do not know whether this is the actual design that will be implemented or not.

If I any of the above details are incorrect I hope Joe or someone else will correct me.  :)

Matthew

> 
> 
>> 
>> 
>>> On Jan 3, 2016, at 9:10 PM, Matthew Johnson <matthew at anandabits.com <mailto:matthew at anandabits.com>> wrote:
>>> 
>>> 
>>>> On Jan 3, 2016, at 9:08 PM, Drew Crawford <drew at sealedabstract.com <mailto:drew at sealedabstract.com>> wrote:
>>>> 
>>>>> Existentials for protocols with Self and / or associated type requirements would require bindings for Self and / or the associated type(s).  At least when you use a member that contains Self and / or an associated type in its signature.  So the previous example will always fail to compile. 
>>>> 
>>>> Not true.  Joe Groff:
>>> 
>>> Can you point me to the source?  I would like more context around these comments.
>>> 
>>>> 
>>>>> This seems like it would be addressed just by allowing Factory to be used as a dynamic type, with its Product type generalized to Any. We'll be set up to support that with some runtime work to store associated types in protocol witness tables (which is also necessary to fix cyclic conformances, one of our Swift 3 goals).
>>>> 
>>>> 
>>>>> Yeah, when generalizing a protocol type, we ought to be able to either generalize the associated types to their upper bounds, for use cases like yours, or constrain them to specific types, for the AnyGenerator<T> kind of case.
>>> 
>> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160104/b766a3b9/attachment.html>


More information about the swift-evolution mailing list