[swift-evolution] protocol can only be used as a generic constraint because it has Self or associated type requirements
marc at knaup.koeln
Mon Dec 14 08:21:30 CST 2015
@Andrew sure you could provide an init implementation for a protocol. It
would be like a factory for that protocol similar to class clusters in
Actually a proposal I'm working on depends on that being possible somehow :)
On Mon, Dec 14, 2015 at 3:02 PM, Andrew Bennett <cacoyi at gmail.com> 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
> 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>
> 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
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>> swift-evolution mailing list
>> swift-evolution at swift.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution