Hello!
I am encountering some behavior using generics with protocols & adding generic constraints to conformances of the protocol.
**note I am running Xcode 10 & Swift 4.2
tldr;
//Implements a function in ObserverType protocol with a generic constraint
func did<T>(start operation: T) where T : Async {
print(#function + " T : Async")
}
func did<T>(start operation: T) {
print(#function)
}
the following code:
var observers : [ObserverType] = []
operation.observers.forEach { $0.did(start: operation) }
results in the following function being called
func did<T>(start operation: T) {
print(#function)
}
Without the generic constraints? I would think that if I define a function with generic constraints that it would be called?
The interesting thing is if I call:
obs.did(start: operation)
directly, the generic constraints function will be called.
I tried to condense the context as much as possible, hopefully this illustrates the issue. Appreciate any insights / solutions/ workarounds.
I also have a gist with the full code (copy and paste into a Playground):
Cheers