What is the most concise, unambiguous syntax for operator associated methods (for overloading etc.) that doesn't pollute the namespace?

Posted by Doug Treadwell on Programmers See other posts from Programmers or by Doug Treadwell
Published on 2011-09-13T01:05:10Z Indexed on 2012/11/01 23:20 UTC
Read the original article Hit count: 187

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.

© Programmers or respective owner

Related posts about programming-languages

Related posts about language-design