Massive Issue: Operator overloading

56 views
Skip to first unread message

Josh Leverette

unread,
Oct 11, 2011, 10:08:17 PM10/11/11
to General Dart Discussion
This is a major problem. Humongous. Deal breaker almost. I understand
the reasoning for not allowing multiple function overloads, but
operators... I really need to be able to do multiple. For instance, I
was building a geometrical library earlier using Dart and I had a
class type Point. Point obviously stored a location. I needed to be
able to add and subtract points, which would directly add the x-es and
the y-s together independently and create a new point. But, I needed
to also be able to do translations where I could add a number to a
point and it would add that number to both x and y. That's when I got
an error. I don't care if I have to go into a strictly typed mode or
whatever, but not being able to do that was like taking the pristine
beautiful code I had written up until that point and beating it with a
sledgehammer. Suffice it to say things lose their beauty really
quickly under the direct force of a sledgehammer.

John Tamplin

unread,
Oct 11, 2011, 11:01:26 PM10/11/11
to Josh Leverette, General Dart Discussion
When you add something to a Point, if overloading can help then clearly you know what type you are calling it with.  Why is it hard to name it according to what you are actually doing, ie:

class Point {
   ...
   void offsetXY(int dist) { x+=dist; y+=dist; }
   Point addPoint(Point p) { return new Point(x+p.x, y+p.y); }
}

You could also use dynamic type checks if you really want overloading:

Point operator+(var op) {
  switch (true) {
    case op is num:
       // add scalar
    case op is Point:
       // add points
    ...
  }
}

--
John A. Tamplin
Software Engineer (GWT), Google

Josh Leverette

unread,
Oct 11, 2011, 11:04:32 PM10/11/11
to General Dart Discussion
because I've never used any code like that before, which means I'm
very unlikely to think of it unless it's mentioned somewhere related
to overloading. My use of dynamically typed languages is very limited
up to this point, it's quite a different paradigm, but I'm only so
verbal about this language because I see an insane amount of potential
in it. This language has many features I've wanted for a very long
time in other languages.

On Oct 11, 10:01 pm, John Tamplin <j...@google.com> wrote:

Josh Leverette

unread,
Oct 11, 2011, 11:05:57 PM10/11/11
to General Dart Discussion
(code like the second example to be exact, what you showed at first
was essentially the only solution I came up with on my own)

Alan Knight

unread,
Oct 11, 2011, 11:17:59 PM10/11/11
to John Tamplin, Josh Leverette, General Dart Discussion
One pattern for this is "double dispatch"

Basically the Point's + operation will then turn around and call the function
   argument.addPoint(this)
at which point the type of one argument is determined by the function name and the other argument knows what type it is. It costs one more function call, but can be fairly clean.



John Tamplin
11 October, 2011 11:01 PM



When you add something to a Point, if overloading can help then clearly you know what type you are calling it with.  Why is it hard to name it according to what you are actually doing, ie:

class Point {
   ...
   void offsetXY(int dist) { x+=dist; y+=dist; }
   Point addPoint(Point p) { return new Point(x+p.x, y+p.y); }
}

You could also use dynamic type checks if you really want overloading:

Point operator+(var op) {
  switch (true) {
    case op is num:
       // add scalar
    case op is Point:
       // add points
    ...
  }
}

--
John A. Tamplin
Software Engineer (GWT), Google


Josh Leverette
11 October, 2011 10:08 PM

John Tamplin

unread,
Oct 11, 2011, 11:26:19 PM10/11/11
to kni...@acm.org, Josh Leverette, General Dart Discussion
On Tue, Oct 11, 2011 at 11:17 PM, Alan Knight <kni...@acm.org> wrote:
One pattern for this is "double dispatch"

Basically the Point's + operation will then turn around and call the function
   argument.addPoint(this)
at which point the type of one argument is determined by the function name and the other argument knows what type it is. It costs one more function call, but can be fairly clean.

That also assumes you control the other type, for example "int" or "num" in the case of  adding a scalar to a point, as you need to define Point num.addToPoint(Point p).

Florian Bösch

unread,
Oct 12, 2011, 6:15:39 AM10/12/11
to General Dart Discussion
On Oct 12, 5:26 am, John Tamplin <j...@google.com> wrote:
> That also assumes you control the other type, for example "int" or "num" in
> the case of  adding a scalar to a point, as you need to define Point
> num.addToPoint(Point p).
Which does imply monkey-patching int (not a good idea as is evidenced
in ruby and various builtin prototype overriding JS frameworks like
Prototype).

I've got mixed feelings about overloading, and I know it can be a pain
to implement. I do think though that a simple, optional, opt-in,
builtin multi-dispatch mechanism would be beneficial. You can still do
the other things like typeswitches and double dispatch, but at least
you had a quick way to do dispatches without much fuss.

Seldaiendil D. Flourite

unread,
Oct 12, 2011, 1:23:59 PM10/12/11
to General Dart Discussion
Reply all
Reply to author
Forward
0 new messages