-
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
Dictionary expressions: update key-value pair conversions #8850
Merged
Merged
Changes from 1 commit
Commits
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 |
---|---|---|
|
@@ -193,24 +193,42 @@ An implicit *collection expression conversion* exists from a *collection express | |
* `System.Collections.Generic.IDictionary<TKey, TValue>` | ||
* `System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>` | ||
|
||
*Collection expression conversions* require implicit conversions for each element. The element conversion rules are now differentiated based on whether the *element type* of the target type is `KeyValuePair<,>`. | ||
*Collection expression conversions* require implicit conversions for each element. | ||
The element conversion rules are updated as follows. | ||
|
||
If the *element type* is a type *other than* `KeyValuePair<,>`, the rules are *unchanged* from *language version 12* other than **clarifications**: | ||
|
||
> An implicit *collection expression conversion* exists from a collection expression to a *type* with *element type* `T` **where `T` is not `KeyValuePair<,>` and** where for each *element* `Eᵢ` in the collection expression: | ||
> The implicit conversion exists if the type has an *element type* `T` where for each *element* `Eᵢ` in the collection expression: | ||
> * If `Eᵢ` is an *expression element*, there is an implicit conversion from `Eᵢ` to `T`. | ||
> * If `Eᵢ` is a *spread element* `..Sᵢ`, there is an implicit conversion from the *iteration type* of `Sᵢ` to `T`. | ||
> * **Otherwise there is *no implicit conversion* from the collection expression to the target type.** | ||
> * **If `Eᵢ` is a *key-value pair element* `Kᵢ:Vᵢ` and `T` is a type `KeyValuePair<K, V>`, there is an implicit conversion from `Kᵢ` to `K` and an implicit conversion from `Vᵢ` to `V`.** | ||
> * **Otherwise there is *no conversion* from the collection expression to the target type.** | ||
|
||
### Key-value pair conversions | ||
|
||
A *key-value pair conversion* is introduced. | ||
|
||
If the *element type* is `KeyValuePair<,>`, the rules are *modified* for *language version 13* (this applies to any type with an *element type* of `KeyValuePair<,>`, not only *dictionary types*): | ||
An implicit *key-value pair conversion* exists from an expression of type `KeyValuePair<T1, T2>` to a type `KeyValuePair<U1, U2>` | ||
if there is an implicit conversion from `T1` to `U1` and an implicit conversion from `T2` to `U2`. | ||
|
||
> An implicit *collection expression conversion* exists from a collection expression to a *type* with *element type* `KeyValuePair<K, V>` where for each *element* `Eᵢ` in the collection expression: | ||
> * If `Eᵢ` is an *expression element*, then the type of `Eᵢ` is `KeyValuePair<Kᵢ:Vᵢ>` and there is an implicit conversion from `Kᵢ` to `K` and an implicit conversion from `Vᵢ` to `V`. | ||
> * If `Eᵢ` is a *key value pair element* `Kᵢ:Vᵢ`, there is an implicit conversion from `Kᵢ` to `K` and an implicit conversion from `Vᵢ` to `V`. | ||
> * If `Eᵢ` is a *spread element* `..Sᵢ`, then the *iteration type* of `Sᵢ` is `KeyValuePair<Kᵢ:Vᵢ>` and there is an implicit conversion from `Kᵢ` to `K` and an implicit conversion from `Vᵢ` to `V`. | ||
> * Otherwise there is *no implicit conversion* from the collection expression to the target type. | ||
An explicit *key-value pair conversion* exists from an expression of type `KeyValuePair<T1, T2>` to a type `KeyValuePair<U1, U2>` | ||
if there is an implicit or explicit conversion from `T1` to `U1` and an implicit or explicit conversion from `T2` to `U2`. | ||
|
||
Implicit key-value pair conversions are useful for *expression elements* and *spread elements* where the key or value types do not match exactly. | ||
Despite the name, key-value pair conversions *do not* apply to *key-value elements*. | ||
|
||
```csharp | ||
Dictionary<int, string> x = ...; | ||
Dictionary<long, object> y = [..x]; // implicit conversion from KVP<int, string> to KVP<long, object> | ||
``` | ||
|
||
Key-value pair conversions are similar to *tuple conversions* that allow converting between distinct tuple types. | ||
That said, a tuple conversion is [*defined*](https://github.com/dotnet/csharpstandard/blob/draft-v8/standard/conversions.md#10213-implicit-tuple-conversions) as a conversion from a *tuple expression* rather than a tuple type, despite being allowed in conversions like `b = a` below, so perhaps conversions between `KeyValuePair<,>` *types* should not be supported. | ||
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. See csharpstandard/issues 1155 |
||
|
||
```csharp | ||
(int, string) a = ...; | ||
(long, object) b = a; // implicit conversion from (int, string) to (long, object) | ||
``` | ||
|
||
The new rules above represent a breaking change: For types that are a valid conversion target in *language version 12* and have an *element type* of `KeyValuePair<,>`, the element conversion rules change between language versions 12 and 13. | ||
*Should *key-value pair conversions* apply to expressions of `KeyValuePair<,>` type in any context, or only for collection expression elements? Are there other conversions that have limited contexts where they apply?* | ||
|
||
## Create methods | ||
|
||
|
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.
Does the explicit conversion matter for us? Or is now for parity/completeness with existing conversions?
Trying to figure out how it would matter for CEs
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.
The explicit conversion is for completeness and consistency with other conversions.
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.
Sounds good :)