Suppose I have the following protocols:
public protocol Iter0 {
associatedtype Elem
func next() -> Elem
}
public protocol Iter1 {
associatedtype Elem
func next() -> Elem
}
How do I conform to both protocols unambiguously in one type? For example:
public class Foo: Iter0, Iter1 {
public typealias Iter0.Elem = Int // can't do this
public typealias Elem:Iter0.Elem = Int // can't do this
public func next() -> Int { // whose next is this?
return 0
}
public func Iter1.next() -> Bool { // can't do this
return 0
}
}
Am I missing a way to do this or is this a legitimate language ambiguity?
Since we're working with interfaces, we can support both protocols simultaneously if we use the same type for "both versions" of Elem. This only successfully works if the semantics of Iter0.next and Iter1.next can work simultaneously and your implementation of next actually does satisfy both at the same time. (For example, Set.isEmpty satisfies that property for Collection and SetAlgebra.)
If you need each protocol's rendition of Elem (and/or next) to differ, then at least one of the interfaces needs to behind a (computed) property of Foo with a custom referencing type.