Consider these protocols
protocol NamedThing{
var name:String{ get }
}
protocol ValuedThing{
associatedtype ValueType
var value:ValueType{ get }
}
And these structs...
struct TestThingA : NamedThing {
let name = "TestThing"
}
struct TestThingB : ValuedThing {
typealias ValueType = Int
let value = 4
}
struct TestThingC : NamedThing, ValuedThing {
typealias ValueType = Int
let name = "TestThing"
let value = 4
}
I'm trying to write an extension that would only apply to struct TestThingC
because it adheres to both protocols.
None of these work, of course...
extension NamedThing & ValuedThing{
func test(){
print("Named thing \(name) has a value of \(value)")
}
}
extension Any where NamedThing & ValuedThing {
func test(){
print("Named thing \(name) has a value of \(value)")
}
}
extension Any where Self is NamedThing, Self is ValuedThing{
func test(){
print("Named thing \(name) has a value of \(value)")
}
}
extension Any where Self == NamedThing, Self == ValuedThing{
func test(){
print("Named thing \(name) has a value of \(value)")
}
}
So how does one write an extension that applies to items which adhere to both (multiple) protocols?