operator overloading

2003-04-09
2013-05-02
  • Nobody/Anonymous

    Does Nice support operator overloading like c# en c++?

     
    • Daniel Bonniot

      Daniel Bonniot - 2003-04-09

      Yes, you can overload operators. They can be defined like any function/method. You just need to enclose their name between backquotes:

      MyMatrix `+`(MyMatix m1, MyMatrix m2) { ... }

       
      • Ross

        Ross - 2006-08-11

        Can you pass an operator as a parameter?

         
        • B S

          B S - 2006-08-21

          Yes, an operator is a normal function.
          For example:
          list.foldLeft(`+`)

          `+` is the name of the function, nice just translates a+b into `+`(a,b), which can lead to error messages that look different from what someone might expect.

          `+`(a,b) is legal IIRC, and is just like a+b

           
    • Nobody/Anonymous

      Why are operators for build-in types treated different than 'custom' types?

       
      • Daniel Bonniot

        Daniel Bonniot - 2003-04-09

        I'm not sure what you mean by that. The point of operator overloading is that build-in types are not treated differently from user types. If you look at the source of the nice.lang package, you can see the definition of the "built-in" operators, similarly to the way you can define your own operators.

        The only special thing is that some operators, like the addition of numbers, are not implemented as a method call, but are inlined with the corresponding bytecode. Actually, you could do that in your own programs, you would just need to implement the class that emits the bytecode.

        Maybe the confusion came from the backquotes. You need them during the definition of an operator, bot when you use it. So if you defined addition on your MyMatrix class as in my previous post, you can use
        MyMatrix m1, m2;
          MyMatrix m3 = m1 + m2;

         
    • Nobody/Anonymous

      The confusion came from the backquotes. I thought I have to use them always. So Matrix m = m1 '*' m2, but this is not the case.

       
    • Nobody/Anonymous

      The confusion came from the backquotes. I thought I have to use them always. So Matrix m = m1 '*' m2, but this is not the case.

       

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks