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

Dictionary expressions: update key-value pair conversions #8850

Merged
merged 3 commits into from
Dec 31, 2024
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
42 changes: 30 additions & 12 deletions proposals/dictionary-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>`
Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good :)

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.
Copy link
Member Author

Choose a reason for hiding this comment

The 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

Expand Down