Jump to content

Talk:Operator overloading

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by JCrenshaw (talk | contribs) at 06:23, 15 December 2008 (What is an operator?). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

clarification: static vs dynamic

Doesn't overloading usually refer to the static resolution of a call? For instance, + on int vs String in Java is resolved statically. A dynamic analog of overloading is something like a type-case or virtual methods. So I think this article should focus on the static case, which means that some of the examples, like LISP, do not feature overloading, because they are statically untyped. —Preceding unsigned comment added by 12.198.139.122 (talk) 01:57, 6 February 2008 (UTC)[reply]

I removed this text:

Similarly, and with operators in PostgreSQL, where mathematical transformations can be defined on operators and PostgreSQL may use many optimalizations to expressions that use them.

Because I couldn't decipher what the original author was trying to express. If someone can clarify the intent of this passage and wants to put the content back into the article, please do so. Neilc 02:26, 11 Aug 2004 (UTC)


more than implicit aesthetics

Operator overloading provides more than an aesthetic benefit when the language allows operators to be invoked implicitly in some circumstances. For example, this is the case with to_s operator in Ruby, which returns a string representation of an object.

This doesn't make sense to me. How is it more than aesthetic? Is there other difference if you just write the invocation? I think the point is there might be a difference between function call semantics and operator semantics (such as a + b might choose between a.add and b.add)

it depends upon if you assume that there exist other overloadable methods/functions/whatever or not. If operator overloading is the only overloading, it does add more than aesthetic.

By the way, you could add Lisp to the language classification :-)

Ok, I removed this until someone can explain how it makes sense. --TuukkaH 15:40:05, 2005-09-07 (UTC)

Declaring new operators

What is meant by "declaring new operators"? 62.194.114.150 10:20, 28 Nov 2004 (UTC)

Using operator symbols in the language that have no default meanings, like an operator *** or ## for instance. Pico allows this. Wouter Lievens 10:48, 6 Mar 2005 (UTC)

Criticism

The criticism counts for all language features, so it is a red herring, imho.

Example:

a.addToList(b); // this function call sends an e-mail to Bill Gates

This is also an example of giving something a bad name (or bad semantics). The criticism is thus irrelevant. Wouter Lievens 10:48, 6 Mar 2005 (UTC)

It may be a red herring, but it is also the commonest criticism of overloading. The purpose of Wikipedia is to describe things as they are, not to evaluate them: our job is to report what critics actually say, relevant or not, not to try to evaluate whether things are good or bad ourselves. Haeleth 14:49, September 6, 2005 (UTC)

First language with Operator Overloading

It would be interesting to identify the first language to introduce operator overloading. Anyone know if any language before ALGOL 68 (1968) implemented it?

Rwendland 02:05, 6 Apr 2005 (UTC)

Gutter talk?

I've removed the "gutter talk" section, which (if you can't be bothered to check History) was about how what absolutely everyone calls "operator overloading" should really be called "operator overriding" unless a class exports more than one operator. Given that this is not a common criticism, nor is it supported by actual usage, I don't really think it's encyclopaedic content. (It was also of disputable accuracy.) Haeleth 14:49, September 6, 2005 (UTC)

Lisp

In lisp, (+ 3 5) adds 3 & 5. Lisp uses the same syntax for functions: (funcname arg1 arg2) etc. Because of this, defining new operators in lisp is as easy as defining a new function. Overriding an operator is the same as defining a new function. Because of this, I changed lisp's position in the chart to indicate that operators are overloadable and new operators are definable.--Dasunt 23:27, 3 March 2006 (UTC)[reply]

This is true only if you can have the function + converted into a generic, so you are later able to overload it for different classes. If you only override the previous function definition of the symbol + you're not overloading it. I think it can be done, but am not sure. --euyyn 22:25, 2 July 2006 (UTC)[reply]
As Lisp has dynamic typing and first class functions this is trivially true. You could assign the old implementation to some value, and then define a new function that checks the type of the argument and applies eighter the old implementation or a new one depending on the result. (CLOS also has inbuilt generic functions, but you dont realy need them to do overloading in lisp).

Ruby

How is it that Ruby permits new operators to be defined? It seems to me that only existing operators that are part of the syntax can be defined as methods in Ruby. If that's not what that axis of the chart means, perhaps it should be clarified.

You're right. We can't define new operators in Ruby. There's a limited set (language's operators, except the following: =, .., ..., !, not, ||, &&, and, or, ::). I'm going to remove it from the list. 01:02, 18 February 2007 (UTC)

Dubious statement regarding symbols and citations needed

The ISO/IEC 2382 Standard Vocabularies for IT define an operator as a symbol that identifies or represents an operation. The definition does not limit symbols to single characters. Thus '+', 'add', 'first', 'next' could all be operators.

It is in fact the symbol that is overloaded through reuse. While, hopefully, the operations have similar semantics, the operations are in fact different.

A more accurate statement would be: "Operators are not limited to single characters." As it is, this article appears to contradict a fundamental computing definition and to confuse a symbol with what it represents.

The article lacks citations for verifiability and runs the risk of violating No Original Research or No Point of View.

Bob Badour 15:22, 29 June 2006 (UTC)[reply]

Come on, in the sentence, the word symbol refers to what everybody understand as a symbol, i.e., a graphical/printed sign (in opposition to letters and numbers), not to the special meaning we in the field of programming also have for it. --euyyn 22:47, 2 July 2006 (UTC)[reply]
Clearly not everyone understands the word symbol the same. It should be clarified to show the intended meaning, perhaps with something like "punctuation characters," "graphical symbols," or "non-alphabetic characters."--69.181.83.91 16:06, 9 July 2006 (UTC)[reply]
Isn't the statement referring to something like C++'s conversion operators? 213.38.7.224 12:33, 7 July 2006 (UTC)[reply]

samples for expression reduction?

are there any known languages that support expression reduction (outside of the compiler)? —The preceding unsigned comment was added by 80.129.243.120 (talk) 23:45, 17 January 2007 (UTC).[reply]

Correction with Java

Java allows method overloading, hence it should be moved to another part of the graph. --indil 08:48, 11 April 2007 (UTC)[reply]

Java allows only method overloading, that is different to *operator* overloading. 78.86.131.114 (talk) 18:24, 13 August 2008 (UTC)[reply]

What is an operator?

Which properties decide about the classification as operator. IMHO there are some points:

  • An operator can consist of letters or special characters.
  • There should be no extra characters required around the operator.
  • An operator can be classified as prefix, infix or postfix.
  • An operator has a priority (or precedence) and an associativity.

There are several notations which do not count as operators:

  • A C function which is called with "cos(x)" requires parentheses and has no priority or associativity.
  • A Lisp function which is called with "(cos x)" requires parentheses and has no priority or associativity.
  • A Smalltalk method which is called with "b at: 3" requires a colon and has no priority or associativity.
  • A Java method which is called with "collection.remove(obj)" requires a dot, parentheses and has no priority or associativity.
  • It is also not an operator when the operator symbol is required to be surrounded by dots (e.g.: FORTRAN .AND. ).

In the article several me too languages claim to have operators (IMHO they don't). Such languages should be mentioned in other articles but not here.

When there is no clear distinction about what counts as operator some information like the table about user definable / overloadable operators make not really sense. Only a few languages use real operators and allow user definable new operator symbols with priority and associativity. Because of the many me too languages this information is not clearly visible. Georg Peter 07:17, 30 October 2007 (UTC)[reply]

I think the key point is that operators have implicit associativity and priority rules. The notatonal convention is irrelevant. As such, Lisp doesn't have operators at all, it has only functions. I think some distinction has to be made between function/method overloading and operator overloading. --Tweenk (talk) 12:53, 17 November 2007 (UTC)[reply]
I'm inclined to agree with Georg Peter on the issue of "priority and associativity" only, however, I can't find any good reliable supporting documentation. In fact, even C++, which should be the poster child for "what defines an operator" says that "sizeof" (usage: sizeof(var);) is an operator. Conversely, however, C++ does not allow you to define new operators (not to be confused with overloading operator new) implying that my own functions are not considered operators (making sizeof an oddball, since it looks like a function, but is somehow an operator?) There appears to be no standard on this other than the arbitrary whims of the folks that designed any particular language.
I think the real question here is "What does the programmer mean by Operator Overloading?" This isn't terribly hard to decide on. If my language of choice calls something an operator, but allows me to redefine how it works, I am overloading it. Creating a custom operator is totally different from overloading an existing operator (the topic of this article.) If we stick to overloading only, we can focus only on those symbols that a language already calls "operators" and avoid the headache of having to define (and fight over the definition of) what actually constitutes an "operator." This won't resolve the question, but it would relocate it to an article more suited to hashing it out (like operator.) --JCrenshaw (talk) 06:23, 15 December 2008 (UTC)[reply]