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

operator overloading for type-closed generic class #1668

Open
aka-nse opened this issue Jun 27, 2018 · 1 comment
Open

operator overloading for type-closed generic class #1668

aka-nse opened this issue Jun 27, 2018 · 1 comment

Comments

@aka-nse
Copy link

aka-nse commented Jun 27, 2018

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 method op_Addition.

BTW, the following code is valid:

using System;

public struct Container<T>
{
    public T Value { get; }
    
    public Container(T value) => Value = value;

    public static Container<T> operator+(Container<T> lhs, Container<T> rhs)
        => new Container<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));
    */
}

public static class Program
{
    public static void Main()
    {
        var a = new Container<int>(1);
        var b = new Container<int>(2);
        var c = 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;

public struct Container<T>
{
    public T Value { get; }
    
    public Container(T value) => Value = value;

    public static Container<int> operator+(Container<int> lhs, Container<int> rhs)
        => new Container<int>(FieldProvider<int>.Current.Add(lhs.Value, rhs.Value));
}

public static class Program
{
    public static void Main()
    {
        var a = new Container<int>(1);
        var b = new Container<int>(2);
        var c = a + b;
        Console.WriteLine(c.Value);
    }
}

although the following code is valid:

using System;

public struct Container<T>
{
    public T Value { get; }
    
    public Container(T value) => Value = value;

    public static Container<int> op_Addition(Container<int> lhs, Container<int> rhs)
        => new Container<int>(FieldProvider<int>.Current.Add(lhs.Value, rhs.Value));
}

public static class Program
{
    public static void Main()
    {
        var a = new Container<int>(1);
        var b = new Container<int>(2);
        var c = 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.

@iam3yal
Copy link
Contributor

iam3yal commented Jun 29, 2018

#164 should solve this and more.

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

2 participants