Skip to content

Allow ref struct on Generic ThrowHelper methods #1127

Open

Description

@Avid29

Overview

This would allow ref structs to be used in pattern matching with a ThrowHelper case, using the new allow ref struct "anti-constraint".

API breakdown

This changes nothing except allowable use cases for existing ThrowHelper APIs.

Usage example

I encountered a situation in my code where I wanted to pattern match and return a ref struct. I found I could not use the ThrowHelper because it was a ref struct.

Here's an example of another imaginable situation utilizing the Span<T>.

Span<int> span = x switch
{
    0 => stackalloc int[size],
    1 => new int[size],
    2 => something.AsSpan(),
    _ => ThrowHelper.ThrowArgumentOutOfRangeException<Span<int>>(),
};

Breaking change?

No

Alternatives

throw without ThrowHelper

Additional context

This will require .NET 9 and C# version 13

Help us help you

Yes, I'd like to be assigned to work on this item

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request 📬A request for new changes to improve functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions