Skip to content
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

Closed
petarpetrovt opened this issue Aug 25, 2020 · 6 comments
Closed

[Proposal] Unique generic constraint #3822

petarpetrovt opened this issue Aug 25, 2020 · 6 comments

Comments

@petarpetrovt
Copy link

Add support for unique generic constraint that ensures some or all generic type parameters are unique at compile time.

public View<T1, T2> View<T1, T2>()
    // where T1 != T2
    // OR: where T1 is not T2
    // OR: where typeof(T1) != typeof(T2)
    // OR: where unique
{
    // Runtime checks are the only current alternative.
    Assert.NotSame<T1, T2>();
}
@petarpetrovt petarpetrovt changed the title [Proposal] Unique generic constraint types [Proposal] Unique generic constraint Aug 25, 2020
@ZacharyPatten
Copy link

ZacharyPatten commented Aug 25, 2020

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 "is" keyword, because the current usage of "is" does respect polymorphism.

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:

// OR: where T1 is not T2

@svick
Copy link
Contributor

svick commented Aug 25, 2020

Why?

@petarpetrovt
Copy link
Author

@ZacharyPatten
I don't really care about polymorphism support (my use case all types are value types with struct constraint). The proposed syntax is optional.

@PathogenDavid
Copy link

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.

@PathogenDavid
Copy link

PathogenDavid commented Aug 26, 2020

Also this is a duplicate of #1706 (which also presents a scenario where such a constraint enables you to do something new that an analyzer can't help with.)

Somewhat related: #707, #3560

@333fred
Copy link
Member

333fred commented Aug 26, 2020

Closing as a duplicate.

@333fred 333fred closed this as completed Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants