Roslyn: Generic operators support in C#.

Created on 18 Dec 2015  路  6Comments  路  Source: dotnet/roslyn

Currently c# does not support generic operators definition like that:

``` C#
public static MyAddExpression operator+(SomeClass c, T t){return ...}

This support can be usefull to build advanced libs with expressions (validation expressions in my case).

This is only C# limitation, CLR ready for this feature.
We can already define operator in different syntax.

``` C#
[SpecialName]
public  static string op_Addition(SomeClass c, string s){...}

No changes in CLR needed to define generic operator in this syntax:

C# [SpecialName] public static MyAddExpression<SomeClass, T> op_Addition<T>(SomeClass c, T t){return ...}

but later C# does not recognize it.

Please add this proposal to the C# 7.0 wish list.

0 - Backlog Area-Language Design Feature Request

Most helpful comment

I would love this feature. Non-generic operators are limiting.

All 6 comments

I would love this feature. Non-generic operators are limiting.

Related to #3391 and #2147.

Yes. In my opinion this is now absolutely necessary. Having only generics that are not fully equivalent to C++ generics (in functional behaviour) are absolutely mandatory. I guess there are many guys out there requesting this feature. Instead a lot of people here are complaining about syntactic sugar.

Might help solve the problem of verbose generic patterns discussed in #10153. If is was generic the pattern could infer its generic type.

I have needed this feature trying to model mathematical entities and it is a definite must. It will save tons of repetitive code and make the conceptual model richer. Also type inference would do wonders in this scenario.

Issue moved to dotnet/csharplang #813 via ZenHub

Was this page helpful?
0 / 5 - 0 ratings