In Swift, from what I understand, protocols describe attributes which can apply to a data structure. Protocol extensions then, allow for those attributes to be defined per data structure to which they apply.
If that is true, why does the following error appear:
invalid redeclaration of 'invalid'
On this line:
extension CausesError where Self: Example { var invalid: Bool { return true } }
In this code:
struct Example: CausesError { }
protocol CausesError { var invalid: Bool { get } }
extension CausesError where Self: Example { var invalid: Bool { return true } }
Just to synthesize what @dfri said for those who see this later The actual error was caused by:
Because Example is a struct, and therefore has no Self property.
The problem was that I had a fundamental misconception about protocol extension.
Therefore, what I should have done (in order to provide default implementation at a protocol level, even to data types) was simply: