[swift-evolution] Enhanced existential types proposal discussion

Matthew Johnson matthew at anandabits.com
Mon May 23 12:20:08 CDT 2016

> On May 23, 2016, at 11:08 AM, Austin Zheng via swift-evolution <swift-evolution at swift.org> wrote:
>> I think that *all* methods should be available - at least in principle - with associated types 
>> - replaced by their upper bounds (i.e. Any if no constraints have been given either by the protocol definition itself or th existential) if in covariant position and 
>> - replaced by their lower bounds if in contravariant position
>> As it is not possible to define lower bounds in Swift, the lower bounds are always the bottom type (called `Nothing` in Swift and not be confused with optionals). The bottom type has no members and therefore a method referencing that type cannot be called and is effectively not available.
> Thanks for the feedback! So methods that have associated types in contravariant position would have those types be Nothing, unless there was a concrete type bound to that associated type in the Any's where clause?
> Example
> protocol MyProtocol {
>   associatedtype AssocType1
>   associatedtype AssocType2
>   func foo(x: AssocType1, y: AssocType2)
> }
> let a : Any<MyProtocol>
> // on 'a', foo is exposed as 'foo(x: Nothing, y: Nothing)', and can thus not be called
> let b : Any<MyProtocol where .AssocType1 == Int>
> // on 'b', foo is exposed as 'foo(x: Int, y: Nothing)' and still can't be called
> let c : Any<MyProtocol where .AssocType1 == Int, .AssocType2 == String>
> // on 'c', foo is exposed as 'foo(x: Int, y: String)', and can therefore be called
> Let me know if this is what you had in mind.

From the standpoint of the interface of the existential these methods are not callable.  That is the most important point and your proposal already covers this.

If contravariance were ever introduced into Swift we would want to include updates to existentials in the process of adding it.

> Austin
>> -Thorsten 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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

More information about the swift-evolution mailing list