You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In C#, operator overloading is supported as syntax sugar of static method calling.
For example, operator+ will be converted to the static method op_Addition.
BTW, the following code is valid:
using System;publicstructContainer<T>{publicTValue{get;}publicContainer(Tvalue)=>Value=value;publicstaticContainer<T>operator+(Container<T>lhs,Container<T>rhs)=>newContainer<T>(FieldProvider<T>.Current.Add(lhs.Value, rhs.Value));/* is equivalent to : public static Container<T> op_Addition(Container<T> lhs, Container<T> rhs) => new Container<T>(FieldProvider<T>.Current.Add(lhs.Value, rhs.Value)); */}publicstaticclassProgram{publicstaticvoidMain(){vara=newContainer<int>(1);varb=newContainer<int>(2);varc=a+b;/* is equivalent to: var c = Container<int>.op_Addition(a, b); */
Console.WriteLine(c.Value);}}
but the following code is not valid:
using System;publicstructContainer<T>{publicTValue{get;}publicContainer(Tvalue)=>Value=value;publicstaticContainer<int>operator+(Container<int>lhs,Container<int>rhs)=>newContainer<int>(FieldProvider<int>.Current.Add(lhs.Value, rhs.Value));}publicstaticclassProgram{publicstaticvoidMain(){vara=newContainer<int>(1);varb=newContainer<int>(2);varc=a+b;
Console.WriteLine(c.Value);}}
although the following code is valid:
using System;publicstructContainer<T>{publicTValue{get;}publicContainer(Tvalue)=>Value=value;publicstaticContainer<int>op_Addition(Container<int>lhs,Container<int>rhs)=>newContainer<int>(FieldProvider<int>.Current.Add(lhs.Value, rhs.Value));}publicstaticclassProgram{publicstaticvoidMain(){vara=newContainer<int>(1);varb=newContainer<int>(2);varc=Container<int>.op_Addition(a, b);
Console.WriteLine(c.Value);}}
Like this, type-closed generic static method is available but such a operator is prohibited now.
In this proposal I suggest to allow operator overloading for type-closed generics such as 2nd code.
The text was updated successfully, but these errors were encountered:
Maybe related to #1315.
In C#, operator overloading is supported as syntax sugar of static method calling.
For example,
operator+
will be converted to the static methodop_Addition
.BTW, the following code is valid:
but the following code is not valid:
although the following code is valid:
Like this, type-closed generic static method is available but such a operator is prohibited now.
In this proposal I suggest to allow operator overloading for type-closed generics such as 2nd code.
The text was updated successfully, but these errors were encountered: