I've a problem with associated types in Swift, that probably result from my lack of experience. Please consider the following Code, resulting in the inlined error message:
protocol A {
associatedtype TA
}
protocol B {
associatedtype TB
var a: any A { get }
}
extension A {
func methodA(param: TA) {
print(param)
}
}
extension B {
func methodB(param: TB) {
a.methodA(param: param) // Member 'methodA' cannot be used on value of type 'any A'; consider using a generic constraint instead
}
}
class AImpl: A {
typealias TA = String
}
class BImpl: B {
typealias TB = String
var a: any A = AImpl()
}
let b = BImpl()
b.methodB(param: "Hello World")
Now my actual code was much more complex and I do understand now that the compiler of course is unable to ensure TA and TB are of the same type. I however have no idea how to solve this. The probably needs to be a where somewhere, but I got no idea where to even start with these 'generic constraints'. Any suggestions would be much appreciated.
You have to change your abstraction a bit if you want to ensure that the associated type of
A.TAmatchesB.TB.In
B, you need to declare anotherassociatedtype, which conforms toAand declare the propertyato be of this new associated type. Once you do this, you can add awhereclause to themethodBfunction where you can refer toA.TAand make sure that it equalsTB.