I read that Dart does not support function overloading. Does it support operator overloading? If yes, can you show me how it's done in a simple example? And what are some advantages etc?
-
where did you read that *Dart does not support function overloading*? – AlikElzin-kilaka Apr 30 '12 at 07:19
-
@AlikElzin-kilaka, it written at https://dart.dev/guides/language/effective-dart/design#avoid-using-runtime-type-tests-to-fake-overloading for example – Nashev May 02 '22 at 10:23
-
Also, we can see discussion at https://github.com/dart-lang/sdk/issues/49 (Support for method overloading when using typed arguments) – Nashev May 02 '22 at 11:05
6 Answers
The chosen answer is no longer valid when you try overloads using the ==
operator in the new version. Now you need to do this:
class MyClass {
@override
bool operator ==(other) {
// compare this to other
}
}
But it's not safe. other
is not specified as a type, Something unexpected may happened. For example:
void main() {
var a = A(1);
var b = B(1);
var result = a == b;
print(result); //result is true
}
class A {
A(this.index);
final int index;
@override
bool operator ==(other) => other.index == index;
}
class B {
B(this.index);
final int index;
}
So you could do like this:
class A {
A(this.index);
final int index;
@override
bool operator ==(covariant A other) => other.index == index;
}
You need to use covariant
. Because Object overloads the ==
operator.
or you can
Test Object Type:
visit: hash_and_equals
class A {
A(this.index);
final int index;
@override
bool operator ==(other) => other is A && (other.index == index);
@override
int get hashCode => index;
}
-
4Actually we are talking about **overriding** here, as the `@override` decorator suggests. By contrast, **overloading** would allow defining the same method or operator several times in one class, each time with different signatures (i.e. parameter lists). This is not supported by Dart, but can be emulated using `dynamic` or named parameters. – Ber Mar 31 '21 at 07:55
-
Thank's, @Ber! One example of this is a class dart.ui.Size in case of operator -, which described as OffsetBase operator -(OffsetBase other) { if (other is Size) return Offset(width - other.width, height - other.height); if (other is Offset) return Size(width - other.dx, height - other.dy); throw ArgumentError(other); } instead of two operators - with different input operand type and result classes – Nashev May 02 '22 at 08:42
-
1In the [https://dart.dev/guides/language/effective-dart/design#avoid-using-runtime-type-tests-to-fake-overloading](https://dart.dev/guides/language/effective-dart/design#avoid-using-runtime-type-tests-to-fake-overloading) page authors strongly advice to do not use fake overloading through incapsulated runtime type checking and also have note "Dart doesn’t have overloading." – Nashev May 02 '22 at 09:22
Dart does support operator overloading using the operator keyword followed by the operator you want to overload. The following example overloads the == operator for the MyClass object:
class MyClass {
operator ==(MyClass other) {
// compare this to other
}
}
almost all Darts built-in operators can be overloaded with a few notable exceptions being the assignment operator = and reference equivalence operator === (doesn't exist anymore).
As for the advantage of operator overloading, it allows you to reuse operators that have a well known semantic meaning such as == or + for operations on your objects. For example, if you have a Matrix class that overloads the + operator then you can add two matrices using the syntax m1 + m2 instead of the more cumbersome m1.plus(m2)

- 5,867
- 4
- 32
- 56

- 20,275
- 13
- 66
- 83
-
could tell me if 'other' is anything special or could it be named something else. Like ==(MyClass ggg) – Muhammad Umer Apr 13 '12 at 01:05
-
1@MuhammadUmer other could be named anything, its just the parameter name of the class object you are comparing to i.e. this == other or this + other – Lars Tackmann Apr 13 '12 at 05:13
-
Does the argument have to be the same type, or can one create operators like Scala's List cons operator :: , where the type to the left is the element type to make the new head, and the type to the right is the List type? – pohl Oct 26 '12 at 15:56
-
For common pitfalls to watch out for when overriding **==** see http://work.j832.com/2014/05/equality-and-dart.html – Günter Zöchbauer May 03 '15 at 16:47
-
To extend Lars' answer, you can also overload operators using the inline function syntax.
class MyClass {
operator ==(MyClass o) => id == o.id;
}

- 47,944
- 19
- 150
- 166
A amazing example to learn how to use operator overloading is a class to handle complex numbers in dart:
import 'dart:core';
class Complex {
final double real;
final double imaginary;
Complex({this.real = 0, this.imaginary = 0});
Complex.ri(this.real, this.imaginary);
Complex operator +(Complex b) {
return Complex(
real: this.real + b.real, imaginary: this.imaginary + b.imaginary);
}
Complex operator -(Complex b) {
return Complex(
real: this.real - b.real, imaginary: this.imaginary - b.imaginary);
}
Complex operator *(Complex b) {
return Complex(
real: this.real * b.real - this.imaginary * b.imaginary,
imaginary: this.real * b.imaginary + this.imaginary * b.real);
}
Complex operator /(Complex b) {
// https://stackoverflow.com/a/41146661/6846888
var conjugation = b.conjugate();
var denominatorRes = b * conjugation;
// denominator has only real part
var denominator = denominatorRes.real;
var nominator = this * conjugation;
return Complex(
real: nominator.real / denominator,
imaginary: nominator.imaginary / denominator);
}
bool operator ==(b) {
return b.real == this.real && b.imaginary == this.imaginary;
}
@override
String toString() {
return 'Complex(real: ${real}, imaginary: ${imaginary})';
}
}

- 8,463
- 2
- 36
- 37
Since Dart version 2.7 you can add operators to existing classes, for example:
extension Contains on String {
bool operator <<(Pattern other) => contains(other);
bool operator >>(String other) => other.contains(this);
}

- 10,588
- 4
- 64
- 86
Using the Complex numbers as sample, I can implement "Complex * num" like:
Complex operator *(dynamic b) {
if (b is Complex) {
return Complex( real: ... );
} else if (b is num) {
return Complex.ri(real*b, imaginary*b);
}
}
...
But how implement "num * Complex"? Is possible?

- 367
- 1
- 3
- 3