1

The question title is hard to digest but you'll see what I mean when you look at the code sample. I am trying to get class D to conform to protocol C by using protocol B as the type alias. I thought this would be ok, since B also conforms to A, which is the constraint defined in the associated type in C, but the compiler throws an error. Is what I'm trying to do not possible?

protocol A { }
protocol B: A { }

protocol C {
    associatedtype T: A
}

class D: C {
    typealias T = B
}

Note: it works if B is a class instead of a protocol.

Dalton Sweeney
  • 1,108
  • 3
  • 11
  • 24

1 Answers1

0

Overview

  • Not sure if you really need associated type
  • Use the base protocol instead

Code

protocol A { }
protocol B: A { }

protocol C {
    
    func f1(something : A)
}

class D : C {
    
    func f1(something: A) { }
}


class X : B {}

let d1 = D()

d1.f1(something: X())
Community
  • 1
  • 1
user1046037
  • 16,755
  • 12
  • 92
  • 138