#4: Protocols with Self Requirements
Question
// Swift Quiz
— objc.io (@objcio) March 13, 2019
struct USD: Equatable {
var cents: Int
static func ==(l: USD, r: USD) -> Bool {
return l.cents == r.cents
}
}
let x: Equatable = USD(cents: 100)
// Does this compile?
Solution
Solution: Does not compile
— objc.io (@objcio) March 14, 2019
The Equatable protocol has Self requirements. Therefore, it can only be used as generic constraint.https://t.co/s7500CWmAg