Unable to provide method level constraints for class level generic parameters. #1621
Replies: 16 comments
-
scala use :< and :> to constraints type,can c# support this feature? |
Beta Was this translation helpful? Give feedback.
-
In the CLR the generic type constraints are a part of the generic type parameter and they cannot be defined separately, nor can the constraints be narrowed/modified by a different scope (e.g. method or nested type). In Scala the generics are erased during compile time so the language can be a little looser with the rules. In C# the generic arguments are not erased. To change how the generic type constraints work in C# would require modifications to the runtime to support those kinds of constraints. |
Beta Was this translation helpful? Give feedback.
-
Suppose that byte[] BuildPack<THeader, TBody>(THeader header, TBody body)
where TPack : IPack<string, THeader, TBody>; |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Something like
to
I'm not sure if I could also easily be missing something.
Eh, I've changed my mind/this is wrong. This wouldn't be so easy, because existing references to |
Beta Was this translation helpful? Give feedback.
-
This proposal boils down to allowing class C<T>
{
void M() where T : {constraint}
{
}
} which desugars into class C<T>
{
void M<T*>() where T* : T, {constraint}
{
}
} But without any further changes, the call site would still require the type parameter: MyClass mc = ...
mc.M<MyClass>(); I doubt that #1349 would automatically be able to infer the type parameter. But if it could, then we could reduce to mc.M(); or at least mc.M<>(); |
Beta Was this translation helpful? Give feedback.
-
@bondsbw if the compiler is covering over |
Beta Was this translation helpful? Give feedback.
-
@VisualMelon Right, any usage of the constrained type in the method would require a cast from So to fix that issue we would need an "equals" type constraint, e.g. |
Beta Was this translation helpful? Give feedback.
-
@bondsbw yup, that's pretty much the measure of my assessment. |
Beta Was this translation helpful? Give feedback.
-
@bondsbw Yes, compile error at the call site if the TPack does not implement IPack<string, THeader, TBody>,this is what my expect. |
Beta Was this translation helpful? Give feedback.
-
Next version can improve this issue? |
Beta Was this translation helpful? Give feedback.
-
I use extension methods to address this in some limited scenarios. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Up to today,When will this issue be solved? |
Beta Was this translation helpful? Give feedback.
-
@SarcoZ As of today, I'm not aware of a plan to solve this issue. |
Beta Was this translation helpful? Give feedback.
-
@bondsbw next version can give this proposal first-class support? this can reduce the number of generic parameters |
Beta Was this translation helpful? Give feedback.
-
@SarcoZ This proposal has not been championed, and changing the CLR has an extremely high bar even compared to language changes. So probably never, to be realistic. Not to say it is a bad proposal. Just that it needs a change which could break forward compatibility in the CLR and it requires a huge commitment from multiple design and implementation teams. |
Beta Was this translation helpful? Give feedback.
-
Can the title of this issue be updated to clearly reflect that its about lower bound type constraints? (Per the closure reason at dotnet/coreclr#20505?) I'd find this feature useful, and it'd be nice for the title here to be more direct about what this issue actually is, so that interested folks can find it more easily and show their support. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
some generic interface like this:
public interface IPack
{
///
/// Key
///
TKey Key { get; }
}
Is this can be support?
Beta Was this translation helpful? Give feedback.
All reactions