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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions proposals/dictionary-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,29 @@ Options include:
2. Use the target type implementation of `IDictionary<K, V>.this[K] { get; set; }`.
3. Use the accessible indexer that matches the signature `V this[K] { get; set; }`.

### `dynamic` elements

Related to the previous question, how should the compiler bind to the indexer when the element expression has `dynamic` type, or when either the key or value is `dynamic`?

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.


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.

```csharp
KeyValuePair<int, string> x = new(1, "one");
KeyValuePair<dynamic, string> y = new(2, "two");

Dictionary<long, string> d;
d = [x]; // compile-time key-value pair conversion from int to long?
d = [y]; // runtime conversion from dynamic to long?
```

Options include:
1. No support for dynamic conversion of the element expression, or of key or value.
2. Rewrite dynamic conversion from element expressions as explicit conversion to element typee.
3. Allow dynamic conversion of element expression, and of key or value.

### Key-value pair conversions

Should the compiler support a new [*key-value pair conversion*](#key-value-pair-conversions) within collection expressions to allow implicit conversions from an expression element of type `KeyValuePair<K1, V1>`, or a spread element with an iteration type of `KeyValuePair<K1, V1>` to the collection expression iteration type `KeyValuePair<K2, V2>`?
Expand Down