-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: ImmutableDictionary.SetItems(params ValueTuple<TKey, TValue>[] Values) #71092
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsBackground and motivationIt would be really nice if the API Proposalnamespace System.Collections.Generic;
public class ImmutableDictionary<TKey, TValue>
{
public ImmutableDictionary<TKey, TValue> SetItems(params ValueTuple<TKey, TValue>[] Values);
} API Usagevar NewValues = OldValues.SetItems(
(DateFieldName.Path_CreatedAt, NewCreatedAt),
(DateFieldName.Path_UpdatedAt, NewUpdatedAt),
(Type_CreatedAt, NewCreatedAt),
(Type_UpdatedAt, NewUpdatedAt)
); Alternative DesignsThis could be made an extension method or left out altogether. RisksNone that I can think of.
|
Have you thought about how often this would be used? Eg perhaps some looking in https://Grep.app ... as you know, there needs to be sufficient need to add a new API, particularly when it can be a simple extension method. Is there a reason this would be particularly valuable on ImmutableDictionary (eg perf) - it does not exist on |
Hi @danmoseley - With dictionary, the overhead is constant regardless of how many items are added. |
@TonyValenti You can largely achieve the same effect yourself by writing an extension method that uses the dictionary's builder: public static ImmutableDictionary<TKey, TValue> SetItems<TKey, TValue>(
this ImmutableDictionary<TKey, TValue> dictionary, params ValueTuple<TKey, TValue>[] values)
{
var builder = dictionary.ToBuilder();
foreach (var (key, value) in values)
{
builder[key] = value;
}
return builder.ToImmutable();
} When I compare this using a simple benchmark based on your example code, the results I get are:
I.e. setting the items one by one is indeed relatively bad, especially when it comes to allocations. But an extension |
@TonyValenti does @svick sound like a reasonable solution? If so can this issue be closed? I'm also personally not a fan of adding helper methods for every possible use case |
This issue has been marked |
Background and motivation
It would be really nice if the
SetItems
API allowed me to useparams
and value tuples to easily specify items that need to be updated.API Proposal
API Usage
Alternative Designs
This could be made an extension method or left out altogether.
Risks
None that I can think of.
The text was updated successfully, but these errors were encountered: