Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does dart support operator overloading

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?

like image 491
Muhammad Umer Avatar asked Apr 12 '12 19:04

Muhammad Umer


People also ask

Does Dart have operator overloading?

Similarly, dart also has this operator overloading feature. Hence, it helps add objects, perform division of objects, etc.

Which are overridable operators?

New operators such as ** , <> , or &| cannot be created. It is not possible to change the precedence, grouping, or number of operands of operators. The overload of operator -> must either return a raw pointer, or return an object (by reference or by value) for which operator -> is in turn overloaded.

Does ruby have operator overloading?

Ruby permits operator overloading, allowing one to define how an operator shall be used in a particular program. For example a '+' operator can be define in such a way to perform subtraction instead addition and vice versa.


3 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;
}
like image 99
Coeus.D Avatar answered Oct 23 '22 14:10

Coeus.D


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)

like image 31
Lars Tackmann Avatar answered Oct 23 '22 12:10

Lars Tackmann


To extend Lars' answer, you can also overload operators using the inline function syntax.

class MyClass {
  operator ==(MyClass o) => id == o.id;
}
like image 8
Daniel Imms Avatar answered Oct 23 '22 12:10

Daniel Imms