-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[Proposal] Unique generic constraint #3822
Comments
How would this work with polymorphism? Would generic types be considered equal if they are parts of the same branch in an inheritance tree? If this feature would not respect polymorphism, it should never use the " using System;
public class Program
{
public static void Main()
{
A a = new A();
B b = new B();
Method(a, b);
}
public static void Method<T1, T2>(T1 a, T2 b)
{
Console.WriteLine(a is T2);
Console.WriteLine(b is T1);
}
}
public class A { }
public class B : A { } Mentioning this because you proposed the possible syntax:
|
Why? |
@ZacharyPatten |
Generic type constraints are encoded in CIL and are understood and enforced by the runtime, which means this request would most likely require a runtime change, which raises the bar significantly. This is something you could/should pretty easily enforce with an analyzer. Constraints are meant to enable you to use new capabilities with a generic type, not to enable your domain-specific usage restrictions. |
Closing as a duplicate. |
Add support for unique generic constraint that ensures some or all generic type parameters are unique at compile time.
The text was updated successfully, but these errors were encountered: