Skip to content

Instantly share code, notes, and snippets.

@swiftyfinch
Created September 23, 2020 07:32
Show Gist options
  • Save swiftyfinch/3475957b9eed084622fbb54af5c74767 to your computer and use it in GitHub Desktop.
Save swiftyfinch/3475957b9eed084622fbb54af5c74767 to your computer and use it in GitHub Desktop.
LazyOnce implementation (for post: https://medium.com/@tinkoffhere/b22f51422345).
@propertyWrapper final class LazyOnce<T> {
var wrappedValue: T {
if let existStorage = storage { return existStorage }
let newStorage = lazyBlock()
self.storage = newStorage
return newStorage
}
private var storage: T?
private let lazyBlock: () -> T
init(wrappedValue: @escaping @autoclosure () -> T) {
self.lazyBlock = wrappedValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment