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 expression arguments: open questions #9158

Merged
merged 9 commits into from
Mar 13, 2025

Conversation

cston
Copy link
Member

@cston cston commented Feb 19, 2025

No description provided.

@cston cston requested a review from a team as a code owner February 19, 2025 22:11
Comment on lines 403 to 410
Should collection arguments and the applicable methods affect convertibility of the collection expression?

```csharp
Print([with(comparer: null), 1, 2, 3]); // ambiguous or Print<int>(HashSet<int>)?
static void Print<T>(List<T> list) { ... }
static void Print<T>(HashSet<T> set) { ... }
```
Copy link
Contributor

@jnm2 jnm2 Feb 19, 2025

Choose a reason for hiding this comment

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

Print<int>(new(comparer: null)) gives:

CS0121: The call is ambiguous between the following methods or properties: 'C.Print(List)' and 'C.Print(HashSet)'

If that's ambiguous, it would be intuitive to me for Print<int>([with(comparer: null)]) to also be ambiguous. The commonality being target-typing while providing constructor arguments.

Given that, then since the presence of items is unrelated to the differentiation between the overloads, I don't know what the motivating reason would be to make Print([with(comparer: null), 1, 2, 3]) be able to resolve the overload ambiguity.

Copy link
Member

Choose a reason for hiding this comment

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

agreed. and i imagine the same holds true of Print<int>(new(comparer: null) { 1, 2, 3 }).

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. I've added those examples.

public void Add(T t) { ... }
// ...
}
```
Copy link
Member

Choose a reason for hiding this comment

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

similar to above, the following is not legal today: UseMyCollection(new(1));. So i would expect the same to be true with CEs

For either of those cases, arguments are required:
### Collection builder parameter order

For *collection builder* methods, should the elements parameter be before or after any parameters for collection arguments?
Copy link
Member

Choose a reason for hiding this comment

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

@stephentoub for thoughts. Are there different API shapes you want CBA to be able to point to wrt the elemtns vs other arguments?

Copy link
Member

Choose a reason for hiding this comment

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

We effectively have both orderings in the core libraries.

Collection types like Dictionary typically have a constructor that takes a collection of source elements and then a comparer, e.g.
ctor

But Create methods like these:
FrozenSet
ImmutableHashSet
have used the other order, comparer and then elements, so that the elements can be params.

To best take advantage of existing Create methods, we probably want to use the comparer-then-elements ordering (even though I don't particularly like that ordering, especially now that we have collection expressions and params isn't as valuable as it used to be).


For reference, with non-dictionary targets, an element with `dynamic` type, the compiler binds to the applicable `Add(value)` method at runtime.

For dictionary targets, the situtation is more complicated because we have key-value pairs to consider. ...
Copy link
Contributor

Choose a reason for hiding this comment

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

... implies unfinished, but just in case:

Suggested change
For dictionary targets, the situtation is more complicated because we have key-value pairs to consider. ...
For dictionary targets, the situation is more complicated because we have key-value pairs to consider. ...

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. I've since removed the question about dynamic elements and clarified the steps in the Construction section so I think the question no longer applies.


For dictionary targets, the situtation is more complicated because we have key-value pairs to consider. ...

There is a related question of *key-value pair conversions* (see below). If we allow dynamic conversion of key or value independently, then we're essentially supporting key-value pair conversions at runtime. If that's the case, we'll probably want to key-value pair conversions at compile-time for non-`dynamic` cases.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
There is a related question of *key-value pair conversions* (see below). If we allow dynamic conversion of key or value independently, then we're essentially supporting key-value pair conversions at runtime. If that's the case, we'll probably want to key-value pair conversions at compile-time for non-`dynamic` cases.
There is a related question of *key-value pair conversions* (see below). If we allow dynamic conversion of key or value independently, then we're essentially supporting key-value pair conversions at runtime. If that's the case, we'll probably want to do key-value pair conversions at compile-time for non-`dynamic` cases.

@cston cston force-pushed the collection-args-questions branch from fbe0718 to 0d5e1f2 Compare March 13, 2025 15:12
@cston cston requested review from 333fred and CyrusNajmabadi March 13, 2025 15:39
@cston cston merged commit 767d522 into dotnet:main Mar 13, 2025
1 check passed
@cston cston deleted the collection-args-questions branch March 13, 2025 17:41
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.

5 participants