Skip to content

Instantly share code, notes, and snippets.

@cjnevin
Created November 5, 2022 22:15
Show Gist options
  • Save cjnevin/20ec5d1a6acb83a499ba3f92296d6e7f to your computer and use it in GitHub Desktop.
Save cjnevin/20ec5d1a6acb83a499ba3f92296d6e7f to your computer and use it in GitHub Desktop.
@propertyWrapper
public enum Lazy<Value> {
case uninitialized(() -> Value)
case initialized(Value)
public init(wrappedValue: @autoclosure @escaping () -> Value) {
self = .uninitialized(wrappedValue)
}
public var wrappedValue: Value {
mutating get {
switch self {
case .uninitialized(let initializer):
let value = initializer()
self = .initialized(value)
return value
case .initialized(let value):
return value
}
}
set {
self = .initialized(newValue)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment