Skip to content

Instantly share code, notes, and snippets.

@polac24
Created March 31, 2019 18:00
Show Gist options
  • Save polac24/e1b04b6f6cbb51d7ffe6c4ebaf8c7912 to your computer and use it in GitHub Desktop.
Save polac24/e1b04b6f6cbb51d7ffe6c4ebaf8c7912 to your computer and use it in GitHub Desktop.
protocol DefaultProvidable {
static var defaultValue: Self {get}
}
extension Bool: DefaultProvidable {
static let defaultValue = false
}
extension Optional: DefaultProvidable {
static var defaultValue: Optional<Wrapped> {return nil}
}
...
func niceStub<I,O: DefaultProvidable>(of: (I) -> (O)) -> (I) -> (O){
return { _ in O.defaultValue }
}
class DatabaseStub: Database {
lazy var addUserAction = niceStub(of: addUser) // returns false
func addUser(name: String) -> Bool {
return addUserAction(name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment