Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Operator overloading
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Timeline of operator overloading== ===1960s=== The [[ALGOL 68]] specification allowed operator overloading.<ref>{{cite web |title=Report on the Algorithmic Language ALGOL 68, Section 10.2.2. |url=https://www.softwarepreservation.org/projects/ALGOL/report/Algol68_revised_report-AB-600dpi.pdf =Barry J. Mailloux |last3=Peck |first3=John E. L. |author-link3=John E. L. Peck |last4=Koster |first4= Cornelis H. A. |author-link4=Cornelis H.A. Koster |access-date=1 April 2007 |date=August 1968 |display-authors=etal}}</ref> Extract from the ALGOL 68 language specification (page 177) where the overloaded operators ¬, =, ≠, and '''abs''' are defined: 10.2.2. Operations on Boolean Operands a) '''op''' ∨ = ('''bool''' a, b) '''bool''':( a | '''true''' | b ); b) '''op''' ∧ = ('''bool''' a, b) '''bool''': ( a | b | '''false''' ); c) '''op''' ¬ = ('''bool''' a) '''bool''': ( a | '''false''' | '''true''' ); d) '''op''' = = ('''bool''' a, b) '''bool''':( a∧b ) ∨ ( ¬b∧¬a ); e) '''op''' ≠ = ('''bool''' a, b) '''bool''': ¬(a=b); f) '''op''' '''abs''' = ('''bool''' a)'''int''': ( a | 1 | 0 ); Note that no special declaration is needed to ''overload'' an operator, and the programmer is free to create new operators. For dyadic operators their priority compared to other operators can be set: '''prio''' '''max''' = 9; '''op''' '''max''' = ('''int''' a, b) '''int''': ( a>b | a | b ); '''op''' '''++''' = ( '''ref''' '''int''' a ) '''int''': ( a +:= 1 ); ===1980s=== [[Ada (programming language)|Ada]] supports overloading of operators from its inception, with the publication of the Ada 83 language standard. However, the language designers chose to preclude the definition of new operators. Only extant operators in the language may be overloaded, by defining new functions with identifiers such as "+", "*", "&" etc. Subsequent revisions of the language (in 1995 and 2005) maintain the restriction to overloading of extant operators. In [[C++]], operator overloading is more refined than in [[ALGOL 68]].<ref>{{cite web |title=A History of C++: 1979−1991 |url=http://www.stroustrup.com/hopl2.pdf |last=Stroustrup |first=Bjarne |author-link=Bjarne Stroustrup |page=12 |access-date=1 April 2007}}</ref> ===1990s=== [[Java (programming language)|Java]] language designers at [[Sun Microsystems]] chose to omit overloading.<ref>{{cite web |url=http://www.cafeaulait.org/javafaq.html#xtocid1902938 |website=The comp.lang.java FAQ List |title=FAQ Question 6.9: Why isn't there operator overloading?}}</ref><ref>{{cite web |url=http://java.sun.com/docs/white/langenv/Simple.doc2.html |title=java.sun.com |url-status=dead |access-date=26 March 2009 |archive-date=7 March 2009 |archive-url=https://web.archive.org/web/20090307035128/http://java.sun.com/docs/white/langenv/Simple.doc2.html }}</ref><ref>{{cite book |last=Holzner |first=Steven |title=C++: Black Book |year=2001 |publisher=Coriolis Group |location=Scottsdale, Arizona |isbn=1-57610-777-9 |page=387 |quote=One of the nicest features of C++ OOP is that you can overload operators to handle objects of your classes (you can't do this in some other OOP-centric languages, like Java).}}</ref> [[Python (programming language)|Python]] allows operator overloading through the implementation of methods with special names.<ref>{{cite web |url=https://docs.python.org/3/reference/datamodel.html#specialnames |website=The Python Language Reference |title=3. Data Model, Special method names}}</ref> For example, the addition (+) operator can be overloaded by implementing the method {{code|obj.__add__(self, other)}}. [[Ruby (programming language)|Ruby]] allows operator overloading as syntactic sugar for simple method calls. [[Lua (programming language)|Lua]] allows operator overloading as syntactic sugar for method calls with the added feature that if the first operand doesn't define that operator, the method for the second operand will be used. ===2000s=== Microsoft added operator overloading to [[C Sharp (programming language)|C#]] in 2001 and to [[Visual Basic .NET]] in 2003. [[Scala (programming language)|Scala]] treats all operators as methods and thus allows operator overloading by proxy. In [[Raku (programming language)|Raku]], the definition of all operators is delegated to lexical functions, and so, using function definitions, operators can be overloaded or new operators added. For example, the function defined in the [[Rakudo]] source for incrementing a Date object with "+" is: <syntaxhighlight lang="perl6"> multi infix:<+>(Date:D $d, Int:D $x) { Date.new-from-daycount($d.daycount + $x) } </syntaxhighlight> Since "multi" was used, the function gets added to the list of [[multidispatch]] candidates, and "+" is only overloaded for the case where the type constraints in the function signature are met. While the capacity for overloading includes '''+''', '''*''', '''>=''', the [[Imaginary unit|postfix and term '''i''']], and so on, it also allows for overloading various brace operators: "'''['''x, y''']'''", "x'''['''y''']'''", "x'''{''y''}'''", and "x'''('''y''')'''". [[Kotlin (programming language)|Kotlin]] has supported operator overloading since its creation.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)