Created
June 24, 2020 21:24
-
-
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.
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 | |
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? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't like the name. Is
NilOnce
any better? Comments are welcome.