I'm not the original poster but let's assume the logging function is:
func log(_ s: String) {
os_log("-> %@", s)
}
There are still places that we call this with regular strings, so we'll need to add another one:
func log(_ s: Substring) {
os_log("-> %@", String(s))
}
Well, that's not ideal because we are copying the logic in two places, can we merge those into one function? I jump into the definitions and see:
public struct String { ... }
public struct Substring { ... }
So these are plain structs, no common superclass or anything. Maybe there is a common protocol? Well good luck finding it. After some googling, I finally find StringProtocol, this looks promising:
func log(_ s: StringProtocol) {
os_log("-> %@", String(s))
}
That didn't work: Protocol 'StringProtocol' can only be used as a generic constraint because it has Self or associated type requirements
So I guess I have to make log generic?
func log<T: StringProtocol>(_ s: T) {
os_log("-> %@", String(s))
}
Ok that worked! Now repeat this to every other function that may need to handle both String and Substring...
We all knew that it is possible to log a String and a Substring in Swift, the discussion is about it being cumbersome, which I agree that it is.