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] List of allowed types as generic constraint #1629

Closed
Expro opened this issue Jun 13, 2018 · 4 comments
Closed

[Proposal] List of allowed types as generic constraint #1629

Expro opened this issue Jun 13, 2018 · 4 comments

Comments

@Expro
Copy link

Expro commented Jun 13, 2018

Generic type constraint in form of
public static T Extension<T>(this T) where T : InterfaceOne, InterfaceTwo
Means that T must implement both interfaces: InterfaceOne and InterfaceTwo.

Currently, there is no way to define overloaded (extension) method with same signature, but different constraints. For example, following code is not valid:
public static T Extension<T>(this T) where T : InterfaceOne
public static T Extension<T>(this T) where T : InterfaceTwo

Proposal: allow for listing of type constraints, where constaint will be met if generic parameter meets any of listed types. For example, if we assume following code:
public static T Extension<T>(this T) where T in InterfaceOne, InterfaceTwo
Would allow for any object implementing only InterfaceOne, only InterfaceOne or both.

Alternative syntax:
public static T Extension<T>(this T) where T from InterfaceOne, InterfaceTwo

@benaadams
Copy link
Member

public static T Extension<T>(this T) where T : InterfaceOne
public static T Extension<T>(this T) where T : InterfaceTwo

Would be better (perhaps via garbled names under the covers for the clr)

Otherwise it would potentially lead to runtime type casting in a shared generic which would be inefficient vs a compile time link up.

Related #1628

@ufcpp
Copy link

ufcpp commented Jun 14, 2018

Union types?

@YairHalberstadt
Copy link
Contributor

Would definitely aid on defining a method on eg byte, int16, int32, int64, float, double, decimal etc all at once.
However shapes may solve this issue.

@YairHalberstadt
Copy link
Contributor

Closing as a duplicate of #1371

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

4 participants