Skip to content

Instantly share code, notes, and snippets.

@Plnda
Last active May 23, 2020 21:50
Show Gist options
  • Save Plnda/536774890c180ff3f708279a77440d1e to your computer and use it in GitHub Desktop.
Save Plnda/536774890c180ff3f708279a77440d1e to your computer and use it in GitHub Desktop.
@propertyWrapper
struct Injected<Service> {
typealias DelayedInjection = () -> Service
var service: Service?
var delayed: DelayedInjection?
init() {
delayed = { Dependencies.main.resolve() }
}
var wrappedValue: Service {
mutating get {
if let service = service {
return service
} else if let delayed = delayed {
service = delayed()
return service!
} else {
fatalError()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment