Imagine you have a magic mirror that flips whatever you put in front of it. If you stand in front of it, your reflection appears opposite—your left becomes right, and your right becomes left.
Unary operators in Dart work similarly! They perform a single action on one thing—just like the magic mirror flipping only you, not anyone else. In this article, we’ll learn how to customize these operators to change how objects behave.
What Are Unary Operators?
Unary operators are special because they only work with one value. Unlike normal math where you add two numbers (5 + 3
), unary operators only act on a single number or object.
Here are some common unary operators in Dart:
-
(negation) → Turns a number into its opposite.+
(unary plus) → Doesn’t change the number but can be useful in expressions.~
(bitwise complement) → Flips all the bits in a number (used in binary math).++
and--
(increment and decrement) → Increase or decrease a number by 1 (but these cannot be overloaded).
Overloading Unary Operators in Dart
Dart allows us to override unary operators inside a class. This means we can decide what happens when we use an operator on an object. We do this using the operator keyword inside a class.
Let’s look at some examples to see how this works!
Example: Overloading the -
Operator
Imagine a Point in a 2D space, like on a graph. If we overload the -
operator, we can flip the point across the origin, just like the magic mirror example!
Here’s how we do it:
class Point {
int x, y;
Point(this.x, this.y);
// Overloading the - operator
Point operator -() {
return Point(-x, -y); // Flip both x and y
}
@override
String toString() {
return "Point($x, $y)";
}
}
void main() {
Point p1 = Point(3, 4);
print(p1); // Point(3, 4)
Point flipped = -p1; // Using our overloaded - operator
print(flipped); // Point(-3, -4)
}
What’s Happening?
- We created a class Point with
x
andy
values. - We overloaded the
-
operator so that it flips the point across the origin. - When we use
-p1
, it creates a newPoint
with negative values.
Just like the magic mirror, our operator flips the point in the opposite direction!
Example: Overloading the ~
Operator
The ~
operator is a bit tricky. It flips all bits in a number (used in binary math).
Imagine a row of light switches, where 1 means ON and 0 means OFF. If we apply the ~
operator, it flips all switches—turning ON lights OFF and vice versa.
Let’s create a class that represents a binary number and overload the ~
operator:
class BinaryNumber {
int value;
BinaryNumber(this.value);
// Overloading the ~ operator
BinaryNumber operator ~() {
return BinaryNumber(~value); // Flip all bits
}
@override
String toString() {
return value.toRadixString(2); // Convert to binary string
}
}
void main() {
BinaryNumber num1 = BinaryNumber(5); // 5 in binary = 101
print(num1); // 101
BinaryNumber flipped = ~num1;
print(flipped); // Flipped bits (depends on system bits)
}
What’s Happening?
5
in binary is101
.- The
~
operator flips all bits, creating a new number. - This is useful when working with binary values in programming.
Things to Remember
- You can only overload operators inside classes.
- Only existing operators like
-
,+
, and~
can be overloaded. ++
and--
can’t be overloaded in Dart.
Conclusion
You now understand how to overload unary operators in Dart! You’ve learned how to:
- Overload
-
to reverse a point’s position. - Overload
~
to flip bits like a row of light switches. - Use unary operators to customize object behavior.
Try creating your own class and overloading a unary operator! You could design a class that flips colors or directions—the possibilities are endless.