Repository navigation
feat: collection helpers return whether they changed the collection (v4.3.0) - #61
Merged
Merged
Conversation
…v4.3.0) RemoveFromX, and AddToX on sets and dictionaries, now return bool, mirroring the BCL's Remove, HashSet<T>.Add and TryAdd. Callers that branch on the outcome no longer need ISerializableExtensions.Remove or a Contains check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The generated collection helpers return
void, so a caller that needs to know whether a removal happened cannot use them. It falls back toISerializableExtensions.Remove(list, value), or aContainscheck followed byRemoveFromX(two lookups). ModernUO'sCorpse.OnItemLiftedis an example: it removes the lifted item fromEquipItemsand branches on whether it was worn gear.Changes
Return types now mirror the BCL. A helper that can be a no-op returns whether it changed anything:
RemoveFromX(list, set, dictionary)voidbool, likeRemoveAddToXon a setvoidbool, likeHashSet<T>.AddAddToXon a dictionaryvoidbool, likeTryAddAddToXon a list,InsertIntoX,RemoveFromXAt,ReplaceInX,ClearXvoidMarkDirtystill fires only on an actual change, as in 4.2.0.ContainsKey+Addfallback for dictionaries withoutTryAddreturnsbooltoo. No serializable type reaches that branch today, since onlyDictionary<,>has a migration rule.Compatibility
Callers that ignore the result keep compiling. A helper used as a method group for an
Action<T>(e.g.list.ForEach(RemoveFromX)) would no longer compile. ModernUO has no such use.Tests
DataStructureMethodTests.Helpers_TolerateNullAndMarkDirtyOnlyOnChangenow asserts each helper's return value next to the dirty count: null collections, duplicates, missing entries, and successful adds and removes for lists, sets and dictionaries.Helpers_ReportChangesWithoutADirtyTargetruns the helpers on an entity with no dirty target.After merge
Run the
Build and Publish NuGet Packagesworkflow. A ModernUO follow-up then bumps to 4.3.0 and replacesISerializableExtensions.Removecall sites with the generated helpers, starting withCorpse.OnItemLifted.