Created
June 20, 2019 18:41
-
-
Save pteasima/e07b49509b18b0f5aee619af4f855e8d to your computer and use it in GitHub Desktop.
ReadOnce
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
// I wrote this and then decided its an overengineered piece of bullshit code. Just deleting it makes me sad so let it forever take up space on Github | |
// a property that flips back to its default value when it is read | |
// TODO: does this have proper value semantics with the class inside? And do we even care? | |
@propertyWrapper struct ReadOnce<Value> { | |
init(initialValue: Value) { | |
defaultValue = initialValue | |
box = ValueBox(value: initialValue) | |
} | |
private let defaultValue: Value | |
final class ValueBox { | |
init(value: Value) { self.value = value } | |
var value: Value | |
} | |
private var box: ValueBox | |
var value: Value { | |
get { | |
let result = box.value | |
box.value = defaultValue | |
return result | |
} | |
set { | |
box.value = newValue | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment