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

Collection expressions: require Add method callable with a single argument for class and struct types that do not use [CollectionBuilder] #8022

Merged
merged 3 commits into from
Mar 29, 2024

Conversation

cston
Copy link
Member

@cston cston commented Mar 26, 2024

Previously, collection expression conversion required an Add method callable with a single argument of the iteration type if the collection expression has any elements. This is relaxed to an Add method callable with a single argument, without a requirement on the argument type.

…ument for class and struct types that do not use [CollectionBuilder]
@cston cston marked this pull request as ready for review March 26, 2024 20:15
@cston cston requested a review from a team as a code owner March 26, 2024 20:15
@cston cston requested a review from CyrusNajmabadi March 26, 2024 20:16
@cston cston requested a review from MadsTorgersen March 26, 2024 23:36
in which case the *element type* is the *iteration type*
- The *type* has a constructor that can be invoked with no arguments, and the constructor is at least as accessible as the declaring member.
- The *type* has an instance (not an extension) method `Add` where:
- The method can be invoked with a single argument and the corresponding parameter is either by value or `in`.
Copy link
Contributor

@AlekseyTs AlekseyTs Mar 28, 2024

Choose a reason for hiding this comment

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

and the corresponding parameter is either by value or in.

I am not sure where ref-ness restriction came from. Why is it needed? #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably the bullet point should say the following instead:

  • The method can be invoked with a single value (vs. ref/out/in) argument.

- The method can be invoked with a single argument and the corresponding parameter is either by value or `in`.
- If the method is generic, the type arguments can be inferred from the argument.
- The method is at least as accessible as the declaring member.
- In which case the *element type* is the [*iteration type*](https://github.com/dotnet/csharpstandard/blob/draft-v9/standard/statements.md#1395-the-foreach-statement) of the *type*.
Copy link
Contributor

@AlekseyTs AlekseyTs Mar 28, 2024

Choose a reason for hiding this comment

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

This probably shouldn't be a bullet point. This is not one of the conditions #Closed

in which case the *element type* is the *iteration type* of the *type*.
* The *type* has an *[applicable](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#11642-applicable-function-member)* constructor that can be invoked with no arguments, and the constructor is accessible at the location of the collection expression.
* If the collection expression has any elements, the *type* has an instance or extension method `Add` where:
* The method can be invoked with a single argument and the corresponding parameter is either by value or `in`.
Copy link
Contributor

@AlekseyTs AlekseyTs Mar 28, 2024

Choose a reason for hiding this comment

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

and the corresponding parameter is either by value or in.

Similar comment as for params spec #Closed

* The method can be invoked with a single argument and the corresponding parameter is either by value or `in`.
* If the method is generic, the type arguments can be inferred from the collection and argument.
* The method is accessible at the location of the collection expression.
* In which case the *element type* is the [*iteration type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) of the *type*.
Copy link
Contributor

@AlekseyTs AlekseyTs Mar 28, 2024

Choose a reason for hiding this comment

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

Similar comment as for params spec #Closed

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 1)

AlekseyTs added a commit to AlekseyTs/roslyn that referenced this pull request Mar 28, 2024
AlekseyTs added a commit to AlekseyTs/roslyn that referenced this pull request Mar 28, 2024
AlekseyTs added a commit to dotnet/roslyn that referenced this pull request Mar 29, 2024
The main observable change - the original (unconverted) params arguments are used in the process of populating target collection. 

Spec changes: dotnet/csharplang#8022

Fixes #72098
Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 3)

@cston cston merged commit c925991 into dotnet:main Mar 29, 2024
1 check passed
@cston cston deleted the any-add branch March 29, 2024 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants