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

Compiler should produce errors when using disallowed operator overloads #13327

Open
ssrmm opened this issue Nov 30, 2016 · 2 comments
Open

Compiler should produce errors when using disallowed operator overloads #13327

ssrmm opened this issue Nov 30, 2016 · 2 comments
Labels

Comments

@ssrmm
Copy link

ssrmm commented Nov 30, 2016

Currently the Nemerle compiler silently accepts any operator overloads, even if they are not possible. For clarity the compiler should generate errors when an attempt is made to overload any of the operators implemented as macros.

@VladD2
Copy link
Member

VladD2 commented Nov 30, 2016

Give an example, please.

@ssrmm
Copy link
Author

ssrmm commented Dec 1, 2016

For example this compiles fine:

using System.Console;

namespace Test
{
  public module Main
  {
    public Main() : void
    {
      mutable test1 = Test(1);
      def     test2 = Test(2);
      
      test1 += test2;
      
      WriteLine($"Result=$(test1.Int)");
      _ = ReadKey();
    }
  }
  
  [Record]
  public class Test
  {
    public Int : int { get; } 

    // 1st += overload
    public static @+=(left : ref Test, right : Test) : void
    {
      WriteLine("called @+=(ref Test, Test) : void");
      left = Test(left.Int + right.Int)
    }

    // 2nd += overlaod
    public static @+=(left : Test, right : Test) : Test
    {
      WriteLine("called @+=(Test, Test) : Test");
      Test(left.Int + right.Int)
    }
    
    public static @+(left : Test, right : Test) : Test
    {
      WriteLine("called @+(Test, Test) : Test");
      Test(left.Int + right.Int)
    }
  }
}

Output when running:

called @+(Test, Test) : Test
Result=3

Now that I know that += is implemented as macro, I can easily understand the output.

To me the first version of the overload seems the more logical. The second would rather be def result = test1 += test2;. But in any case the macro just produces test1 = test1 + test2 and neither of the defined overloads can ever be called.

As such it would be good to have an error messages for people that don't know the source code and haven't read Nemerle-for-OOP-Programmers-Week-5#Operator_overloading(the only place it is mentioned AFAICT).

In C# any of my attempts to overload operator += resulted in CS1020.

@VladD2 VladD2 added the Bug label Dec 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants