[swift-evolution] protocol can only be used as a generic constraint because it has Self or associated type requirements

Matthew Johnson matthew at anandabits.com
Mon Dec 14 08:23:53 CST 2015


It is definitely possible to solve this problem for associated types by binding or constraining them.  The ML module system has a feature that is roughly analogous to this.  

I think Self is quite a bit trickier, although there is effectively a implicit constraint that Self conforms to the protocol in question so it may be possible to make use of that information.

Sent from my iPad

> On Dec 14, 2015, at 8:02 AM, Andrew Bennett via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I agree that this is an issue, I'd really like to see a language feature to address it.
> 
> In some circumstances you can work around the issue with type-erasure. You implement a generic wrapper (like AnySequence) that can take anything conforming to that protocol and expose it with a single concrete interface.
> 
> I learnt a lot implementing a few of these. There's a good article on it here:
> https://realm.io/news/type-erased-wrappers-in-swift/
> 
> I wouldn't be too surprised if Apple formalises type erasure somehow, the implementation is very mechanical and could be automated. For example: If you have a protocol MyProtocol that has type requirements then perhaps MyProtocol.Any would refer to an automatically generated type erased concrete type. I suppose you could also do:
> 
> typealias AB = protocol<A,B>
> AB.Any
> 
> The main problem with current implementations seems to be, for example: ZipSequence is a SequenceType, but ZipSequence is not an AnySequence.
> 
> Also you cannot necessarily conform to a protocol if it has things like initialiser requirements.
> 
> 
> 
> 
> 
>> On Tue, Dec 15, 2015 at 12:45 AM, Tal Atlas via swift-evolution <swift-evolution at swift.org> wrote:
>> This is the error that gets in my way the most when trying to do protocol oriented programming. It can be super frustrating. That lead me to make a proposal a few weeks back for having generic protocols.
>> 
>> In that proposal some swift core people expressed that they were keenly aware of said pain. And while they didn't deliver details in time or implementation they expressed that there were efforts ongoing to solve it. 
>> 
>>> On Sun, Dec 13, 2015 at 6:55 PM Marc Knaup via swift-evolution <swift-evolution at swift.org> wrote:
>>> Hey guys,
>>> 
>>> I'm looking at Swift 3.0's goal to improve generics.
>>> 
>>> Is there any info yet if and how we will be able to refer to instances of protocols that have associated types?
>>> What is the difficulty in supporting this?
>>> 
>>> Simple examples:
>>> var list = [Hashable]()
>>> var hashable: Hashable = 2
>>> 
>>> Right now all we get is
>>>> protocol 'Hashable' can only be used as a generic constraint because it has Self or associated type requirements
>>> 
>>> Thanks,
>>>   Marc
>>>  
>>>  _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>  
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> 
> _______________________________________________
> 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/20151214/d63e7a3e/attachment.html>


More information about the swift-evolution mailing list