Skip to content

Commit

Permalink
Collection expressions: prefer ReadOnlySpan<T> over Span<T> in overlo…
Browse files Browse the repository at this point in the history
…ad resolution
  • Loading branch information
cston committed Oct 10, 2023
1 parent c19c7c2 commit 16dc6e4
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions proposals/csharp-12.0/collection-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ var z = Extensions.AsImmutableArray([3]); // ok
## Overload resolution
[overload-resolution]: #overload-resolution
[*Better conversion from expression*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#11644-better-conversion-from-expression) is updated to prefer certain target types in collection expression conversions.
[*Better conversion from expression*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#11644-better-conversion-from-expression) is **updated** to prefer certain target types in collection expression conversions.
In the updated rules:
* A *span_type* is one of:
Expand All @@ -440,12 +440,14 @@ In the updated rules:
> Given an implicit conversion `C₁` that converts from an expression `E` to a type `T₁`, and an implicit conversion `C₂` that converts from an expression `E` to a type `T₂`, `C₁` is a ***better conversion*** than `C₂` if one of the following holds:
>
> * `E` exactly matches `T₁` and `E` does not exactly match `T₂`
> * `E` exactly matches both or neither of `T₁` and `T₂`, and `T₁` is a [*better conversion target*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#11646-better-conversion-target) than `T₂` **and the following is *not true*:**
> * **`C₁` and `C₂` are collection expression conversions *and***
> * **`T₁` is a *span_type* and `T₂` is an *array_or_array_interface_or_string_type*, *or* `T₂` is a *span_type* and `T₁` is an *array_or_array_interface_or_string_type***
> * **`C₁` and `C₂` are collection expression conversions, and `T₁` is a *span_type* with *iteration type* `E₁`, and `T₂` is an *array_or_array_interface_or_string_type* with *iteration type* `E₂`, and `E₁` is implicitly convertible to `E₂`**
> * ...
> * **`E` is a *collection expression* and one of the following holds:**
> * **`T₁` is `System.ReadOnlySpan<E₁>`, and `T₂` is `System.ReadOnlySpan<E₂>`, and an implicit conversion exists from `E₁` to `E₂`**
> * **`T₁` is `System.ReadOnlySpan<E₁>` or `System.Span<E₁>`, and `T₂` is `System.Span<E₂>` or an *array_or_array_interface_or_string_type* with *iteration type* `E₂`, and an implicit conversion exists from `E₁` to `E₂`**
> * **`T₁` is not a *span_type*, and `T₂` is not a *span_type*, and an implicit conversion exists from `T₁` to `T₂`**
> * **`E` is not a *collection expression* and one of the following holds:**
> * `E` exactly matches `T₁` and `E` does not exactly match `T₂`
> * `E` exactly matches both or neither of `T₁` and `T₂`, and `T₁` is a [*better conversion target*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#11646-better-conversion-target) than `T₂`
> * `E` is a method group, ...
Examples of differences with overload resolution between array initializers and collection expressions:
```c#
Expand Down

0 comments on commit 16dc6e4

Please sign in to comment.