Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Python tends to add double underscores before its built-in or overloadable operator methods, like __add(), whereas C++ requires declaring overloaded operators as operator + (Thing& thing) { /* code */ } for example. Personally I like the operator syntax because it seems to be more explicit and keeps these operator overloading methods separated from other methods without introducing weird prefix notation. What are your thoughts?

Also, what about the case of built-in methods that are needed for the programming language to work properly? Is name mangling (like adding __ prefix or sys or something) the best solution here? What do you think about having another type of method declaration, like ... "system method" for lack of creativity at the moment.

So there would be two kinds of declarations:

  • int method_name() { ... }
  • system int method_name() { ... }

... and the call would need to be different to distinguish between them.

obj.method_name(); vs obj:method_name(); perhaps, assuming a language where : can be unambiguously used in this situation.

obj.method_name() vs obj.(system method_name)()

Sure, the latter is ugly, but the idea is to make the common case simple and system stuff should be kept out of the way.

Maybe the Objective-C notation of method calls? [obj method_name]?

Are there more alternatives? Please make suggestions.

share|improve this question
"Effective syntax"? What do you mean by "effective"? – S.Lott Sep 13 '11 at 1:56
Effective meaning concise, unambiguous, not ugly, and staying out of the way of the programmer. – Doug Treadwell Sep 13 '11 at 2:00
Please fix the question and the title to reflect your meaning of "concise, unambiguous". Replace "effective", since it's vague. The "staying out of the way of the programmer" is probably just argumentative and seems like it can be left out of the question. – S.Lott Sep 13 '11 at 2:45
Have you looked into Haskell? It has a very nice mechanism for defining and overloading operators. – tdammers Sep 13 '11 at 7:28

5 Answers

If you prefer a more direct style then, as one of the comments on the question points out, Haskell certainly does that. We have "type classes"--which are more like interfaces than classes in OOP--such as Num, a simplified version of which looks something like this:

class Num a where
    (+), (-), (*) :: a -> a -> a
    negate        :: a -> a

Then to overload those functions for a type, you'd write an instance declaration like this (somewhat silly) example for pairs of ints.

instance Num (Int, Int) where
    (x1, y1) + (x2, y2) = (x1 + x2, y1 + y2)
    (x1, y1) - (x2, y2) = (x1 - x2, y1 - y2)
    (x1, y1) * (x2, y2) = (x1 * x2, y1 * y2)
    negate (x, y) = (negate x, negate y)

Note how the functions are defined in a way that looks just like how they're used, which is pretty slick. Now when you write something like foo + bar * baz it works as-is for any type with a Num instance declaration in scope.

Really doesn't get much simpler than that!

share|improve this answer
probably worth mentioning what the brackets around the symbol do, and possibly backticks as well. – jk. Nov 2 '12 at 8:53

The most concise solution I've seen is in Ruby:

def +(other)
  ...
end

for example,

class Vector
  def +(other)
    Vector.new(self.x + other.x, self.y + other.y)
  end
end
share|improve this answer

C++ gets "almost there". The one exception is operator ++(int), and that's because of the C roots. But if your language doesn't have a C-style i++, that problem is gone. You might also dislike operator() because its declarations end up as T operator()(U u). Both could be solved by changing the declaration syntax to match the operator usage. I.e. ::operator (Foo& old)++ { Foo ret = old; ++old; return ret; }

Support methods are best dealt with by using namespaces. ::std in C++ is a good example, although (again) there's the slight issue with ::operator new (should have been ::std::operator new)

share|improve this answer

In Ada, declaration of operator overloading uses double quotes: "+"(...)

share|improve this answer

I found once a small pascal compiler who do something like this:

program MyProgram;

type
  MyDateTime = record
    Year:  integer;
    Month: integer;
    Day:   integer;
  end;

function AddDateTime(a, b: MyDateTime): MyDateTime; operator +;
begin
  Result := ...;
end;

var X, Y, Z, W: MyDateTime;

begin
  X.Year  = 2011;
  X.Month = 1;
  X.Days  = 1;

  X.Year  = 2011;
  X.Month = 1;
  X.Days  = 3;

  (* same process, done with function call, *)
  (* and operation call *)

  Z := X + Y;

  W := AddDateTime(X, Y);
end.

I lost the link, it was a small compiler company. Not Open Source.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.