Skip to content
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

Add AdditionalPropertiesDictionary.TryGetValue<T> #5528

Merged
merged 2 commits into from
Oct 16, 2024

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Oct 16, 2024

Fixes #5526

Microsoft Reviewers: Open in CodeFlow

/// If a non-<see langword="null"/> is found for the key in the dictionary, but the value is not of the requested type but is
/// an <see cref="IConvertible"/> object, the method will attempt to convert the object to the requested type.
/// </remarks>
public bool TryGetValue<T>(string key, [NotNullWhen(true)] out T? value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we explicitly ban nullable type parameters?

Suggested change
public bool TryGetValue<T>(string key, [NotNullWhen(true)] out T? value)
public bool TryGetValue<T>(string key, [NotNullWhen(true)] out T? value) where T : notnull

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does that help? The parameter is already nullable in the case of reference types.

Copy link
Member

@eiriktsarpalis eiriktsarpalis Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It explicitly prevents passing nullable type parameters even if it doesn't change the nullability of the out parameter.

Copy link
Member Author

@stephentoub stephentoub Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but what benefit does that provide here? What errors does it avoid? It seems like it'd only be a hindrance here, eg not being able to use this in a method with an unconstrained T. This T is only ever in an out position, and the out is already nullable regardless of the T.

@stephentoub stephentoub merged commit ef606f2 into dotnet:main Oct 16, 2024
6 checks passed
@stephentoub stephentoub deleted the additionalpropertiesgetvalue branch October 16, 2024 14:59
@github-actions github-actions bot locked and limited conversation to collaborators Nov 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose generic TryGetValue helpers on AdditionalPropertiesDictionary
2 participants