Reputation: 3088
I have two protocols. Each (x) of them contains associatedtype
that requires to confirm another protocol (y) when associatedtype
of this protocol (y) equals Self
(of x).
protocol B {
associatedtype AA: A
where AA.BB == Self
}
protocol A {
associatedtype BB: B
where BB.AA == Self
}
It is no problem to implement those protocols as follows:
class AAA: A {
typealias BB = BBB
}
class BBB: B {
typealias AA = AAA
}
But I can't implement one of them as generic.
class AAA<BBBB: B>: A {
typealias BB = BBBB
}
class BBB: B {
typealias AA = AAA<BBB>
}
As a result, I have an error:
'A' requires the types 'AAA' and 'BBBB.AA' be equivalent
And note:
requirement specified as 'Self' == 'Self.BB.AA' [with Self = AAA]
Sounds understandable. I added the constraint to my generic class.
class AAA<BBBB: B>: A where BBBB.AA == AAA {
typealias BB = BBBB
}
And result
error: type 'AAA' does not conform to protocol 'A'
note: protocol requires nested type 'BB'; do you want to add it?error: type 'BBB' does not conform to protocol 'B'
note: protocol requires nested type 'AA'; do you want to add it?
All my tries to solve it was unsuccessful, they just changed error messages.
Is it possible to do? How?
Upvotes: 1
Views: 86
Reputation: 353
With my current knowledge, I don't think it's possible to do what you want. I was looking for more information and I found some of them about why it's not needed.
Why Swift doesn't have F-Bounded.
Support for F-bounded polymorphism?
Upvotes: 1