Protocol declaration

I looked at this example of protocol declaration and composition

https ://medium. com/@09mejohn/protocol-composition-in-swift-bd7ed6018ac5
Code Block
protocol CanBark {
func bark()
}
protocol CanWaggTail {
func waggTail()
}
struct Dog {
// Protocol Composition
typealias Dependency = CanBark & CanWaggTail
let dogManager: Dependency
func ballFound() {
dogManager.waggTail()
dogManager.bark()
}
}

It compiles even though bark() is not defined.

More important, how do I declare a Dog: I am requested to declare the dogManager parameter (auto completion tells)
Code Block
let mario = Dog(dogManager: <#T##Dog.Dependency#>)

So I try to create
Code Block
let dependency : CanBark & CanWaggTail

But how should I initialise dependency for use for mario ?
Answered by OOPer in 630858022

It compiles even though bark() is not defined.

bark() is declared in protocol CanBark. Its actual definition needs to exist in a type conforming to CanBark.

But how should I initialise dependency for use for mario ?

You need to define a type conforming to Dependency, and pass the instance of the type to dogManager.
Code Block
struct JpDogManager: CanBark, CanWaggTail {
func bark() {
print("ワンワン")
}
func waggTail() {
print("プルプル")
}
}
let mario = Dog(dogManager: JpDogManager())


Accepted Answer

It compiles even though bark() is not defined.

bark() is declared in protocol CanBark. Its actual definition needs to exist in a type conforming to CanBark.

But how should I initialise dependency for use for mario ?

You need to define a type conforming to Dependency, and pass the instance of the type to dogManager.
Code Block
struct JpDogManager: CanBark, CanWaggTail {
func bark() {
print("ワンワン")
}
func waggTail() {
print("プルプル")
}
}
let mario = Dog(dogManager: JpDogManager())


Thanks.
ありがとうございました
Claude31, can you check out my post please?
@JoshCasticTheCoder 
I will look with pleasure if you provide a link to the thread. for which you want feedback ? (I understand it is not this one because you did not post anything here).

You know this forum is very limited when it comes to searching for your own replies.
Protocol declaration
 
 
Q