I'm currently trying to write a logger, and generate a protocol for that logger so I can mock it. This is very simplified version of the logger:
class Logger: LoggerProtocol {
func log(_ message: String, file: String = #file, line: Int = #line) {
print "[\(file): \(line)] \(message)"
}
}
This to me is syntactic sugar for creating two functions:
func log(String)
func log(String, file: String, line: Int)
Unfortunately, Swift does not agree:
protocol LoggerProtocol {
func log(String) **Swift does not consider Logger to implement this method**
func log(String, file: String, line: Int)
}
My pitch is to allow the "default" in a protocol. I.E.:
protocol LoggerProtocol {
func log(String, file: String = default, line: Int = default)
}
This would allow the protocol to more accurately reflect the capabilities of objects that implement it.
As an alternative, making Logger count as implementing LoggerProtocol because it fulfills the call site requirements would also be a reasonable solution:
protocol LoggerProtocol {
func log(String) **Make Swift consider Logger to be implementing this method**
func log(String, file: String, line: Int)
}