[swift-users] Can't implement generic subscript in protocol

Joanna Carter joanna at carterconsulting.org.uk
Fri Sep 15 04:03:01 CDT 2017


Greetings

Now we've got generic subscripts, I thought I'd play…

protocol DataProvider
{
  subscript<itemType>(index: Int) -> itemType { get }
}

class Test : DataProvider // error : Type 'Test' does not conform to protocol 'DataProvider'
{
  subscript(index: Int) -> String
  {
    return "Fred"
  }
}

Simple questions:

1. why does this not compile?
2. should it?
3. is there a workaround without declaring an associated type in the protocol?

Joanna

--
Joanna Carter
Carter Consulting



More information about the swift-users mailing list