Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 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 Reply

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...