Replies: 5 comments 21 replies
-
What's your use case for this? |
Beta Was this translation helpful? Give feedback.
-
specifically i have two wrapper types, one feature rich for classes/structs and a stripped down version for ref structs. now i need a generic extension method that wraps ref structs in the stripped version and the others in the full version without any ambiguity. Of course I can just name it differently but then I have the problem that users might accidentally create a stripped version with a type that supports full functionality. Both versions are fully stack allocated (there is no notable overhead of the full version). So there is no reason to use the stripped version for classes/structs it's just that some features are not available, like obtaining the default equality comparer. |
Beta Was this translation helpful? Give feedback.
-
I think what you may be looking for is generic specialization: class Wrapper<T> where T : allows ref struct
{
// stripped down
public void ForEverything() { ... }
// full featured
public void ForClass() where T : class { ... }
public void ForStruct() where T : struct { ... }
} There probably are discussions (or even issues) requesting this. |
Beta Was this translation helpful? Give feedback.
-
Constraint that restricting scopes only are mostly suitable for analyzers. Constraints built-in into language should enable additional operations under the constraint. Is there any operation you can do with an instance of |
Beta Was this translation helpful? Give feedback.
-
Could better "Bestest Betterness" solve this issue? using System.Runtime.CompilerServices;
"".M();
// Ambiguous before C# 7.3, but now resolvable. (Bestest betterness)
1.M();
// Improving "Bestest Betterness" rule could solve this issue?
((Span<int>)[]).M();
static class Extensions
{
public struct ClassDummy;
public struct StructDummy;
public struct RefStructDummy;
public static void M<T>(this T x, ClassDummy _ = default) where T : class { }
public static void M<T>(this T x, StructDummy _ = default) where T : struct { }
[OverloadResolutionPriority(-1)]
public static void M<T>(this T x, RefStructDummy _ = default) where T : struct, allows ref struct { }
} |
Beta Was this translation helpful? Give feedback.
-
Not sure if I'm missing something but with the new
allow ref struct
thing it should also be possible to create a constrain that only allows ref structs.e.g. I was just trying to add this one of my projects and came across multiple situations where I need different behavior for ref structs compared to normal structs and classes. but right now i can only differenciate between struct and class.
Is this a design oversight? Are there plans to add this in C# 14? Or was this specifically rejected?
Edit for clarity:
Currently this is valid C#:
I want to extend this to also cover ref structs (in my use case i only need ref struct or not ref struct)
Beta Was this translation helpful? Give feedback.
All reactions