I'm trying to wrap my head around typealias
and associatedtype
in Swift and I'm struggling with a specific case.
Basically I want to have a protocol
which defines a method that takes in a parameter that conforms to a protocol
and every other protocol
that conforms to that protocol
(a bit confusing, yes I know). But basically I just want to allow multiple layers of conformance.
So, this is the closest I could get (this compiles):
protocol Bar {}
protocol BarVariant: Bar {}
struct BarVariantImpl: BarVariant {}
protocol Foo {
associatedtype T: Bar
func update(bar: T)
}
struct FooImpl: Foo {
typealias T = BarVariantImpl
func update(bar: T) {}
}
FooImpl().update(BarVariantImpl())
The problem with the above code is that the typealias T = BarVariantImpl
pins down the parameter type to the concrete implementation. I want to allow parameters that conform to BarVariant
instead. My first thought was to do typealias T: BarVariant
instead, but this does not compile.
I'm running Swift 2.2.1
.