Created
November 5, 2022 22:15
-
-
Save cjnevin/20ec5d1a6acb83a499ba3f92296d6e7f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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