Make PersistenceKeyDefault load its default value lazily #3057
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PersistenceKeyDefault
eagerly evaluates itsdefaultValue
. This means every declaration of aShared
value creates a new copy of this value when it's evaluated. Since the normal pattern for persisting shared values is to successfully load from its source, the default value is generated and thrown away without being used.Additionally, creating this default value can have side effects. Consider a testing context where the default value uses the uuid dependency. Here's a persistence key whose default value creates a
Folder
value whose initializer generates an id from the current uuid dependency.Now when running a test that overrides the default with a predetermined value, the creation of the default value has incremented the uuid dependency, even though the value isn't used.
This request modifies
PersistenceKeyDefault.defaultValue
to lazily evaluate only when theload
action fails. It also includes a couple tests to verify the change.