Is there a way to conform to a Swift protocol using default values?
The following example results in a Type 'FunExample' does not conform to protocol 'Example'
:
protocol Example {
func doSomething(with: String)
}
class FunExample: Example {
func doSomething(with: String, butMakeItFun: Bool = true) {
// implementation
}
}
...even though I actually implicitly conform since this is identical to:
protocol Example {
func doSomething(with: String)
}
class FunExample: Example {
func doSomething(with: String) {
self.doSomething(with: with, butMakeItFun: true)
}
func doSomething(with: String, butMakeItFun: Bool) {
// implementation
}
}
Is there a cleaner way to provide conformance using default values? Or am I missing something conceptually when I say that the first example should also pass conformance?
To clarify: I understand why the error is produced. My question is more conceptual (whether Swift should work this way) and whether there is an elegant way around it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…