Skip to content

Instantly share code, notes, and snippets.

@ahmedk92
Created June 24, 2020 21:24
Show Gist options
  • Save ahmedk92/6b84e6eed98d48b9b5ae5ae21bdd9b6b to your computer and use it in GitHub Desktop.
Save ahmedk92/6b84e6eed98d48b9b5ae5ae21bdd9b6b to your computer and use it in GitHub Desktop.
A property wrapper for values that can be initially nil, but once it has a value, it cannot be reset to nil afterwards.
@propertyWrapper
struct GetOnlyOptional<T> {
init(wrappedValue: T?) {
self.wrappedValue = wrappedValue
}
var wrappedValue: T? {
get {
_value
}
set {
guard let newValue = newValue else { return }
_value = newValue
}
}
private var _value: T?
}
@ahmedk92
Copy link
Author

I don't like the name. Is NilOnce any better? Comments are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment