Swift protocol with associatedtype (ambiguous for type lookup)

Viewed 1617

I need to create generic function in protocol with default implementation in extension. It func should work with item as enum:RawRepresentable where RawValue == String always. I tried

protocol RequiresEnum: class {
    associatedtype SectionIdentifierEnum: RawRepresentable // how this add restriction to RawValue == String

    func test<T: SectionIdentifierEnum>(identifier: T) where T.RawValue == String
}

enum RequiresEnumDefault: String {
    case `default`
}

extension RequiresEnum where Self: UIViewController, SectionIdentifierEnum.RawValue == String {

    typealias SectionIdentifierEnum = RequiresEnumDefault

    func test<T: SectionIdentifierEnum>(identifier: T) where T.RawValue == String {
        print(T.rawValue)
    }

}

but i have errors

  • 'SectionIdentifierEnum' is ambiguous for type lookup in this context
  • 'RawValue' is not a member type of 'T'

Any solutions

1 Answers
Related