-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
287f082
Collection expression arguments: open questions
cston c837efb
Updates
cston 3af4fd1
Add open questions for dictionary expressions
cston 1ec2b68
Add dynamic question
cston e9cc48f
Add example; remove dynamic question
cston 74f6d2b
Conversions: include existing rules
cston 5942f56
Construction updates
cston b7c4f72
Clarify
cston 0d5e1f2
Indexer requires set accessor
cston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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. ... | ||||||
|
||||||
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 was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
```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>`? | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:There was a problem hiding this comment.
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.