Created
September 23, 2020 07:32
-
-
Save swiftyfinch/3475957b9eed084622fbb54af5c74767 to your computer and use it in GitHub Desktop.
LazyOnce implementation (for post: https://medium.com/@tinkoffhere/b22f51422345).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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