I want to implement a printable function for my protocol in the protocol extension, then any struct which conformed the protocol will be printed based on the fields of protocol. But I got the following error, it seems swift does not allow this. Should I extend String
and add a init(_:Info) {...}
for it?
protocol Info {
var name: String { get }
var serial: Int { get }
}
struct Person : Info {
var name = ""
var serial = 0
}
extension Info : CustomStringConvertible {
^~~~~~~~~~~~~~~~~~~~~~~
error: extension of protocol 'Info' cannot have an inheritance clause
var description: String {
return "\(name) + \(serial)"
}
}
func print(info: Info) {
print(info)
}