Skip to content

Instantly share code, notes, and snippets.

@gbasile
Created June 27, 2018 15:38
Show Gist options
  • Save gbasile/1d179d3af955562a66eb3a9f849b7d68 to your computer and use it in GitHub Desktop.
Save gbasile/1d179d3af955562a66eb3a9f849b7d68 to your computer and use it in GitHub Desktop.
Be careful when providing default implementations to your protocols
import Foundation
protocol People {
var eat: String { get } // 1) try to comment this
var dislike: String { get } // 2) Try to comment this
}
//3) Try to rename one of the above
extension People {
var eat: String {
return "Pineapple Pizza"
}
var dislike: String {
return "Vespa"
}
}
struct Giuseppe: People {
var eat: String {
return "Margherita"
}
var dislike: String {
return "Pineapple"
}
}
let giuseppe: Giuseppe = Giuseppe()
print("Real Giuseppe eat: \(giuseppe.eat)")
print("Real Giuseppe dislike: \(giuseppe.dislike)")
let fakeGiuseppe: People = Giuseppe()
print("Fake Giuseppe eat: \(fakeGiuseppe.eat)")
print("Fake Giuseppe dislike: \(fakeGiuseppe.dislike)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment