dart-lang / language

Design of the Dart language
Other
2.65k stars 202 forks source link

Complex arithmetic and scalar operator overloading #1040

Open zringelume opened 4 years ago

zringelume commented 4 years ago

The following features would make numerics in Dart much more pleasant

  1. Complex type
  2. Operator overloading with scalars

Kahan has some nice examples on pages 11 and 50 https://people.eecs.berkeley.edu/~wkahan/JAVAhurt.pdf

lrhn commented 4 years ago

The complex type is easy, that's just a normal class.

The issues are with allowing convenient overloaded syntax, so Complex.operator+ can accept both a number and a Complex as operand, or allowing syntax like x + i * y, because that makes the receiver of + a normal number that knows nothing of complex numbers.

I don't see that being easy without completely changing how operators work in Dart. Currently they are virtual instance methods on the first operand. A design like C#'s where operators are static declarations on either operand's type could allow Complex to declare an Complex operator +(int x, Complex y). That approach also requires some amount of type-based overloading, so it's not something we can easily add to Dart.

It's not an easy thing to support in Dart.