Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
775 views
in Technique[技术] by (71.8m points)

function - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...