Roslyn: Generic types should support operators

Created on 21 Apr 2015  路  2Comments  路  Source: dotnet/roslyn

Generic constraints should be more exact, thus we would be able to use operators on generic types.
for example:
public static T Function1(T a,T b) where T:int,long,uint,ulong
{return a/b+a%b;}

1 - Planning Area-Language Design Feature Request

Most helpful comment

C# is limited by the support for generic constraints offered by the CLR which only really supports class, struct, new(), a subclass and/or a list of interfaces.

Supporting operators would be tricky because operators are both applied to the built-in primitive types, such as the ones you listed, as well as through static methods on any custom type. The IL generated for + could not be the same for System.Int32 v. System.Decimal, but it would have to be.

I want to say that this request is duped (probably multiple times) but I'm having trouble finding an issue number. Perhaps it's still over on CodePlex.

All 2 comments

C# is limited by the support for generic constraints offered by the CLR which only really supports class, struct, new(), a subclass and/or a list of interfaces.

Supporting operators would be tricky because operators are both applied to the built-in primitive types, such as the ones you listed, as well as through static methods on any custom type. The IL generated for + could not be the same for System.Int32 v. System.Decimal, but it would have to be.

I want to say that this request is duped (probably multiple times) but I'm having trouble finding an issue number. Perhaps it's still over on CodePlex.

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

The use case for this would be addressed by concepts: https://github.com/dotnet/csharplang/issues/110

Was this page helpful?
0 / 5 - 0 ratings