Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

+= operator overloading in Dart

Tags:

dart

I trying make the following overloading inside my class:

class Array extends ListBase<double> {
    List<double> l = [];

    List<double> operator +=(List<double> b) {
        var c = Array.length(l.length);
        for(int i = 0; i < l.length; i++) {
          c[i] = this[i] * b[i];
        }
        return c;
      }
}

but the Dart compiler show the error message: the string '+=' ins't a user-definable operator. Is there some way to make the overloading of the operator += for others classes types?

like image 991
Ângelo Polotto Avatar asked Mar 12 '26 12:03

Ângelo Polotto


1 Answers

Overload only operator +. Dart reuse operators that have a well known semantic meaning such as +=. Add @override annotation if operator already defined in base class.

like image 84
Spatz Avatar answered Mar 16 '26 08:03

Spatz