Skip to content

Better conversion: prefer ReadOnlySpan<T> over Span<T>#8779

Merged
cston merged 8 commits intodotnet:mainfrom
cston:prefer-ROS
Jan 14, 2025
Merged

Better conversion: prefer ReadOnlySpan<T> over Span<T>#8779
cston merged 8 commits intodotnet:mainfrom
cston:prefer-ROS

Conversation

@cston
Copy link
Contributor

@cston cston commented Dec 6, 2024

Update better conversion target to prefer ReadOnlySpan<T> over Span<T>.

Follow-up from LDM-2024-12-04 and LDM-2024-12-09.

@cston cston marked this pull request as ready for review December 6, 2024 19:59
@cston cston requested a review from a team as a code owner December 6, 2024 19:59
Copy link
Member

@jjonescz jjonescz left a comment

Choose a reason for hiding this comment

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

just nits that I can follow up on myself;
the rule LGTM

@cston cston requested review from a team, BillWagner and MadsTorgersen December 10, 2024 01:28
> Given two types `T₁` and `T₂`, `T₁` is a ***better conversion target*** than `T₂` if one of the following holds:
>
> - **`T₁` is `System.ReadOnlySpan<E₁>`, `T₂` is `System.Span<E₂>`, and an identity conversion from `E₁` to `E₂` exists**
> - **`T₁` is `System.ReadOnlySpan<E₁>` and `T₂` is `System.ReadOnlySpan<E₂>`, or at least one of `T₁` or `T₂` is not a *span type*, and** an implicit conversion from `T₁` to `T₂` exists and no implicit conversion from `T₂` to `T₁` exists
Copy link
Member

Choose a reason for hiding this comment

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

span type

Should we be explicit this means Span / ReadOnlySpan? I imagine there might be more span types like TensorSpan.

> Given two types `T₁` and `T₂`, `T₁` is a ***better conversion target*** than `T₂` if one of the following holds:
>
> - **`T₁` is `System.ReadOnlySpan<E₁>`, `T₂` is `System.Span<E₂>`, and an identity conversion from `E₁` to `E₂` exists**
> - **`T₁` is `System.ReadOnlySpan<E₁>` and `T₂` is `System.ReadOnlySpan<E₂>`, or at least one of `T₁` or `T₂` is not a *span type*, and** an implicit conversion from `T₁` to `T₂` exists and no implicit conversion from `T₂` to `T₁` exists
Copy link
Member

@jjonescz jjonescz Dec 10, 2024

Choose a reason for hiding this comment

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

I think this could be simplified, i.e., this would have the same effect, right? But maybe it's worse because the intention is less clear.

Suggested change
> - **`T₁` is `System.ReadOnlySpan<E₁>` and `T₂` is `System.ReadOnlySpan<E₂>`, or at least one of `T₁` or `T₂` is not a *span type*, and** an implicit conversion from `T₁` to `T₂` exists and no implicit conversion from `T₂` to `T₁` exists
> - **`T₁` is not `System.Span<E₁>` and** an implicit conversion from `T₁` to `T₂` exists and no implicit conversion from `T₂` to `T₁` exists

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We'll need to allow the case where one type is Span<E1> and the other is not a span type. See similar comment.

@cston cston requested a review from 333fred December 12, 2024 22:34
@cston cston merged commit c905efa into dotnet:main Jan 14, 2025
1 check passed
@cston cston deleted the prefer-ROS branch January 14, 2025 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants