Last active
February 9, 2019 12:13
-
-
Save SpacyRicochet/88a1553ea931e867923d to your computer and use it in GitHub Desktop.
Extension of protocol cannot have inheritance clause
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Money { | |
var value: Double { get } | |
var inGold: Double { get } | |
var symbol: String { get } | |
} | |
// Sadly, gives a compiler error. | |
// "Extension of protocol 'Money' cannot have an inheritance clause" | |
extension Money: CustomStringConvertible { | |
var description: String { | |
return "\(value)\(symbol)" | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This works though. Compiles fine. | |
protocol Money: CustomStringConvertible { | |
var value: Double { get } | |
var inGold: Double { get } | |
var symbol: String { get } | |
} | |
extension Money { | |
var description: String { | |
return "\(value)\(symbol)" | |
} | |
} |
Thanks a lot, @eMdOS. Your solution is correct.
I think you have to let protocol Money
inherit from CustomStringConvertible
in the first place:
protocol Money: CustomStringConvertible {
var value: Double { get }
var inGold: Double { get }
var symbol: String { get }
}
and then, add the following protocol extension:
extension Money {
var description: String {
return "\(value)\(symbol)"
}
}
a working example: struct Diamond: Money
I'm not sure whether I'm right or not, so tell me if I'm wrong.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is because a
constraint
is needed forconformance
purposes, otherwise, the compiler thinks it is aninheritance
.I'm unsure the following code covers your needs, so it could be something like:
I hope this will be helpful.