2

Having a simple class as follows is considered a static warning, why?

operator 'negate' should return numeric type

 
class Vector {
      final int x,y;
      const Vector(this.x, this.y);

      Vector operator +(Vector v) { // Overrides + (a + b).
        return new Vector(x + v.x, y + v.y);
      }

      Vector operator -(Vector v) { // Overrides - (a - b).
        return new Vector(x - v.x, y - v.y);
      }

      Vector operator negate() {    // Overrides unary negation (-a).
        return new Vector(-x,-y);
      }

      String toString() => '($x,$y)';
    }

    main() {
      final v = new Vector(2,3);
      final w = new Vector(2,2);
      assert((-v).x == -2 && (-v).y == -3); // -v  == (-2,-3)
    }

 
adam-singer
  • 4,489
  • 4
  • 21
  • 25

2 Answers2

4

As of 8/21/2012 the Dart Specification Section 7.1.2 Operators, considers defining negate with a possible nullary type as bad style and should report to the user a static warning.

Defining a nullary method named negate or a binary method named equals will have the same effect as dening an operator but is considered bad style, and will cause a static warning.

[...]

It is a static warning if the return type of the user-declared operator []= is explicitly declared and not void. It is a static warning if the return type of the user-declared operator equals is explicitly declared and is not bool. It is a static warning if the return type of the user-declared operator negate is explicitly declared and not a numerical type.

adam-singer
  • 4,489
  • 4
  • 21
  • 25
  • 1
    The spec doesn't look right to me. Why limit the return type of operator negate to a number? You could post it to Dart issues for asking fixes. – Tom Yeh Aug 22 '12 at 02:49
0

We have an open bug for this: http://code.google.com/p/dart/issues/detail?id=3416

Shannon -jj Behrens
  • 4,910
  • 2
  • 19
  • 24