-
Notifications
You must be signed in to change notification settings - Fork 788
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
Type extension issue with allows ref struct
#18001
Comments
This is indeed a regression. The reason why F# does not have syntax for authoring this constraint is because F# has no mechanisms to enforce it. For the case of type extensions we can pretend the constraint is not there, but byref instantiations will then fail at runtime if the usage of T does not permit byref types - I am not sure if there is a way to resolve this issue other than:
|
Okay, it does not sound like there is an (acceptable) easy fix. If that is the case, then I guess we should probably focus our efforts on refactoring our codebase to not use this kind of type extension. Switching to extension methods perhaps. It makes me uneasy but what to do. |
Ugh, it's a nasty issue, unfortunately the closest servicing window is in January |
Just for my understanding, is |
I am thinking if a middle ground is possible:
Would there be some value in this compromise? i.e. the type extension could change the constraints (in this case dropping "allows ref struct"), but the type extension would then reduce availability for consumption compared to plain |
I'm counting 92 type extensions involving generics. Not sure how many members, I'm also not sure how many of these are problematic. But I will start fixing. |
I was especially at the ones being "problematic", i.e. the generic parameter at the interface level in .NET BCL has been marked as "allows ref struct" between .NET 8 and .NET 9. No matter what the outcome is, |
I think there is definitely value in your middle ground suggestion, as a first step. We are using type extensions with |
"Good news everybody..." Our primary codebase is now fixed (apart from test cases), so the damage was quite limited. Also, only |
I am really glad this worked out. |
In our case I changed two properties from type extensions to C#-style extension methods. |
Can the type extension feature be somehow extended, to support extending a subset of instantiation of a type? public static void Foo<T>(this IEnumerable<T> source) where T : class { } It's allowed to specify a narrower constraint for the type parameter. It's also often used effectively as an generic specialization mechanism. |
It is being considered. The difference is that in C# you have to repeat the constraints, otherwise the method is considered to be an unconstrained generic. It's definitely worth a language suggestions, since the need to "add" / "remove" / "replace" constraints does need syntax design as well. |
A type extension on Seq<'T> does not work. The compiler appears to expect 'T to be constrained but I do not know how.
The code below used to work in prior versions of .NET up until .NET 9.0.0-rc2. Starting with .NET 9.0.100 the code is broken. The new compiler breaks helper type extensions at the bottom of our software stack.
Repro steps
Provide the steps required to reproduce the problem:
Expected behavior
Compilation errors.Actual behavior
Compilation errors:
Known workarounds
The text was updated successfully, but these errors were encountered: