- operator in Dart functions as both a binary arithmetic operator for subtraction and a unary prefix operator for numeric negation. Its behavior, precedence, and return type are determined by its arity (the number of operands) and the static types of the expressions involved.
Binary Subtraction
When used as a binary operator,- computes the difference between a left operand and a right operand. It evaluates left-to-right and falls under the additive precedence level.
Type Resolution:
Dart’s type system applies implicit type promotion during binary subtraction based on the num class hierarchy:
int - intevaluates toint.double - doubleevaluates todouble.int - double(or vice versa) evaluates todouble.
Unary Negation
When used as a unary prefix operator,- inverts the algebraic sign of a single numeric operand. It evaluates right-to-left and possesses a higher precedence than binary arithmetic operators.
Type Resolution:
Unary negation preserves the static type of the operand. Negating an int returns an int, and negating a double returns a double.
Operator Overloading
Dart permits the- operator to be overridden in user-defined classes. Because the operator serves two distinct arities, Dart distinguishes the binary and unary implementations entirely by the parameter count in the operator declaration.
- Binary Overload: Defined using
operator -(Type other). It requires exactly one parameter representing the right-hand operand. - Unary Overload: Defined using
operator -(). It requires zero parameters. The compiler distinguishes it from the binary subtraction overload strictly by the absence of parameters. (Note: While the internal symbol name used by the VM fornoSuchMethodisunary-, the actual declaration syntax isoperator -()).
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





