roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Keep ref struct interfaces behind preview flag

Open jaredpar opened this issue 1 year ago • 4 comments

Language design is leaning towards keeping ref struct interfaces for .NET 9. There aren't a lot of real world cases and that is leading to concerns on whether we've battle tested the design enough. Leaving in preview would give us more time to evaluate against real world cases and ensure the design is correct.

This issue is a place holder to ensure we don't attach it to the new language version when it's defined in the 17.12 milestone.

jaredpar avatar Jun 10 '24 20:06 jaredpar

I have some code like this in some scenarios that require very low overhead to be useful:

interface IAction
{
    void Run(/*extra parameters here for some variants of it*/);
}

And I was hoping to update it to allow ref struct IActions so that I didn't have to do the pointer trick in these cases. (Note that I pass the generic TAction by reference to allow state changes to it when it runs)

I will post more use cases when I have a chance to go through more of the code where I ran into similar issues as I find or think of them.

hamarb123 avatar Jun 11 '24 00:06 hamarb123

@hamarb123 Note, the plan is to keep ability for ref structs to implement interface behind a preview flag, the allows ref struct constraint on type parameters is not affected and will be fully supported.

AlekseyTs avatar Jun 11 '24 11:06 AlekseyTs

Related .NET issues

  • https://github.com/dotnet/runtime/issues/103270
  • https://github.com/dotnet/runtime/issues/103605
  • https://github.com/dotnet/runtime/pull/103604

Related Customer Issues

jaredpar avatar Jun 11 '24 17:06 jaredpar

While ref structs implementing interfaces isn't something terribly important to me for .NET 9/C# 13, there is the slightly related feature of being able to mark interface members with [UnscopedRef] that is important and makes a difference in how the .NET Libraries team will ship ITensor<T>

We want to expose ROSpan<nint> Lengths { get; } which for structs (regular -or- ref struct) requires it be annotated as [UnscopedRef] so that you can return an InlineArray or equivalent to allow allocation free tracking of the multidimensional lengths buffer.

tannergooding avatar Jun 18 '24 16:06 tannergooding

LDM confirmed that we want to ship this aspect of the feature in NET 9/C# 13. See https://github.com/dotnet/csharplang/blob/main/meetings/2024/LDM-2024-07-22.md#ref-structs-implementing-interfaces for more information

AlekseyTs avatar Jul 24 '24 14:07 AlekseyTs