I looked at this example of protocol declaration and composition
https ://medium. com/@09mejohn/protocol-composition-in-swift-bd7ed6018ac5
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)
So I try to create
But how should I initialise dependency for use for mario ?
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 ?
bark() is declared in protocol CanBark. Its actual definition needs to exist in a type conforming to CanBark.It compiles even though bark() is not defined.
You need to define a type conforming to Dependency, and pass the instance of the type to dogManager.But how should I initialise dependency for use for mario ?
Code Block struct JpDogManager: CanBark, CanWaggTail { func bark() { print("ワンワン") } func waggTail() { print("プルプル") } } let mario = Dog(dogManager: JpDogManager())