Content deleted Content added
Stevebroshar (talk | contribs) →Operator overloading: Remove info from overloading section that is not about overloading ... and seems low value |
Rescuing 1 sources and tagging 0 as dead.) #IABot (v2.0.9.5 |
||
(34 intermediate revisions by 8 users not shown) | |||
Line 3:
{{About|operators in computer programming|other uses|Operator (disambiguation)}}
In [[computer programming]], an '''operator''' is a [[programming language]] construct that provides functionality that may not be possible to define as a user-defined [[Function (computer programming)|function]] (i.e. [[sizeof]] in [[C (programming language)|C]]) or has [[Syntax (programming languages)|syntax]] different than a function (i.e.
Some operators are represented with symbols {{endash}} characters typically not allowed for a function [[identifier (computer science)|identifier]] {{endash}} to allow for presentation that is more familiar looking than typical function syntax. For example, a function that tests for greater-than could be named <code>gt</code>, but many languages provide an infix symbolic operator so that code looks more familiar. For example, this:
<
<code>if gt(x, y) then return</code>
Can be:
<
Some languages allow a language-defined operator to be overridden with user-defined behavior and some allow for user-defined operator symbols.
Operators may also differ semantically from functions. For example, [[short-circuit evaluation|short-circuit]] Boolean operations evaluate later arguments only if earlier ones are not false.
== Differences from functions ==
== Syntax ==▼
▲=== Syntax ===
Many operators differ syntactically from user-defined functions. In most languages, a function is [[prefix notation]] with fixed [[Order of operations|precedence]] level and associativity and often with compulsory [[parentheses]] (e.g. <code>Func(a)</code> or <code>(Func a)</code> in [[Lisp (programming language)|Lisp]]). In contrast, many operators are infix notation and involve different use of delimiters such as parentheses.
In general, an operator may be prefix, infix, postfix, [[matchfix]], [[circumfix]] or bifix,<ref>{{Cite web|url=https://reference.wolfram.com/language/tutorial/OperatorInputForms.html.en|title=Operator Input Forms—Wolfram Language Documentation|website=reference.wolfram.com}}</ref><ref>{{Cite web|url=
=== Semantics ===
The semantics of an operator may significantly differ from that of a normal function. For reference, addition is evaluated like a normal function. For example, <code>x + y</code> can be equivalent to a function <code>add(x, y)</code> in that the arguments are evaluated and then the functional behavior is applied. However, [[Assignment_(computer_science)|assignment]] is different. For example, given <code>a = b</code> the target <code>a</code> is ''not'' evaluated. Instead its value is replaced with the value of <code>b</code>. The [[scope resolution operator|scope resolution]] and element access operators (as in <code>Foo::Bar</code> and <code>a.b</code>, respectively, in the case of e.g. [[C++]]) operate on identifier names; not values.
In C, for instance, the array indexing operator can be used for both read access as well as assignment. In the following example, the [[increment and decrement operators|increment operator]] reads the element value of an array and then assigns the element value.
<syntaxhighlight lang=c>
</syntaxhighlight>
The C++ <code><<</code> operator allows for [[fluent interface|fluent]] syntax by supporting a sequence of operators that affect a single argument. For example:
<syntaxhighlight lang=cpp>
cout << "Hello" << " " << "world!" << endl;
</syntaxhighlight>
==
{{further|Ad hoc polymorphism}}
A language may contain a fixed number of built-in operators (e.g. {{mono|1=+, -, *, <, <=, !, =}}, etc. in [[Operators in C and C++|C and C++]], [[PHP]]), or it may allow the creation of programmer-defined operators (e.g. [[Prolog]],<ref>{{Cite web|url=https://www.swi-prolog.org/pldoc/man?predicate=op/3|title=SWI-Prolog -- op/3|website=www.swi-prolog.org}}</ref> [[Seed7]],<ref>{{Cite web|url=http://seed7.sourceforge.net/examples/operator.htm|title=Declare an operator|website=seed7.sourceforge.net}}</ref> [[F Sharp (programming language)|F#]], [[OCaml]], [[Haskell]]). Some programming languages restrict operator symbols to special characters like {{mono|1='''[[Addition|+]]'''}} or {{mono|1='''[[Assignment (computer science)|:=]]'''}} while others allow also names like <code>[[Integer_division#Division_of_integers|'''div''']]</code> (e.g. [[Pascal (programming language)|Pascal]]).▼
== Customization ==
Some languages support user-defined [[operator overloading|overloading]] (such as [[C++]] and [[Fortran]]). An operator, defined by the language, can be [[function overloading|overloaded]] to behave differently based on the type of input.
▲
== Examples ==▼
{{category see also|Operators (programming)}}▼
Most languages do not support user-defined operators since the feature significantly complicates parsing. Introducing a new operator changes the arity and precedence [[lexical specification]] of the language, which affects phrase-level [[lexical analysis]]. Custom operators, particularly via runtime definition, often make correct [[static analysis]] of a program impossible, since the syntax of the language may be Turing-complete, so even constructing the syntax tree may require solving the halting problem, which is impossible. This occurs for [[Perl]], for example, and some dialects of [[Lisp (programming language)|Lisp]].
* [[Arithmetic]]: such as addition, <code>a+b</code>▼
* [[Relational operator|relational]]: such as [[Greater-than sign|greater than]], <code>a>b</code>▼
* [[Mathematical logic|Logic]]: such as <code>a AND b</code> or <code>a&&b</code>▼
* [[Assignment (computer science)|Assignment]]: such as <code>a=b</code> or <code>a:=b</code>▼
* [[Record (computer science)|Record]] or [[Object (computer science)|object]] [[Field (computer science)|field]] access: such as <code>a.b</code>▼
* [[scope resolution operator|Scope resolution]]: such as <code>a::b</code> or <code>a.b</code>▼
If a language does allow for defining new operators, the mechanics of doing so may involve meta-programming {{endash}} specifying the operator in a separate language.
* [[Comma operator]]: <code>e, f</code>▼
** [[Elvis operator]]: <code>x ?: y</code>▼
* [[Null coalescing operator]]: <code>x ?? y</code>▼
▲Conceptually, an operator can be [[Function overloading|overloaded]] in the same way that a function can {{endash}} acting differently based on the type of input. Some languages provide operators that are inherently overloaded (aka ''ad hoc polymorphic''). For example, in [[Java (programming language)|Java]] the {{code|+}} operator sums [[number]]s or [[concatenate]]s [[String (computer science)|strings]]. Some languages support user-defined operator overloading (such as [[C++]]).
== Operand coercion ==
{{further|Type conversion}}
Some languages
In
▲== Examples ==
▲{{category see also|Operators (programming)}}
;Mathematical operators
▲* [[Arithmetic]]: such as addition, <code>a {{red|+}} b</code>
▲* [[Relational operator|
▲* [[Mathematical logic|Logic]]: such as <code>a {{red|AND}} b</code> or <code>a {{red|&&}} b</code>
▲* [[Assignment (computer science)|Assignment]]: such as <code>a
* [[Three-way comparison]] (aka spaceship): <code>x {{red|<=>}} y</code>
;Program structure operators
▲* [[Record (computer science)|Record]] or [[Object (computer science)|object]] [[Field (computer science)|field]] access: such as <code>a{{red|.}}b</code>
▲* [[scope resolution operator|Scope resolution]]: such as <code>a{{red|::}}b</code> or <code>a{{red|.}}b</code>
;Conditional operators
* [[Ternary conditional operator|Ternary conditional]]: <code>condition {{red|?}} a {{red|:}} b</code>
▲* [[Null coalescing operator|Null coalesing]]: <code>x {{red|??}} y</code>
;Notable C and C++ operators
* Address-of operator: <code>{{red|&}}x</code>
* [[Dereference operator|Dereference]]: <code>{{red|*}}p</code>
▲* [[Comma operator|Comma]]: <code>e{{red|,}} f</code>
{{anchor|Compound operator|Fused operation}}
▲In the presence of coercions in a language, the programmer must be aware of the specific rules regarding operand types and the operation result type to avoid subtle programming mistakes.
;Compound operators
* [[compound assignment operator|Compound assignment]] (aka augmented assignment) in C/C++: <code>+=</code>, <code>-=</code>, <code>*=</code>, <code>/=</code>, <code>%=</code>, <code><<=</code>, <code>>>=</code>, <code>&=</code>, <code>^=</code>, <code>|=</code>
* [[fused operation|Fused]]: such as [[cis (mathematics)|{{math|1=cis ''x'' = cos ''x'' + ''i'' sin ''x''}}]]
== Operator features in programming languages ==
Line 106 ⟶ 119:
| [[APL (programming language)|APL]]
| {{code|1=+ - × ÷ ⌈ ⌊ * ⍟ <nowiki>|</nowiki> ! ○ ~ ∨ ∧ ⍱ ⍲ < ≤ = ≥ > ≠ . @ ≡ ≢ ⍴ , ⍪ ⍳ ↑ ↓ ? ⍒ ⍋ ⍉ ⌽ ⊖ ∊ ⊥ ⊤ ⍎ ⍕ ⌹ ⊂ ⊃ ∪ ∩ ⍷ ⌷ ∘ → ← / ⌿ \ ⍀ ¨ ⍣ & ⍨ ⌶ ⊆ ⊣ ⊢ ⍠ ⍤ ⌸ ⌺ ⍸}}
| (
| {{Yes}} <small>(first-order functions only)</small>
| {{Yes}}
Line 117 ⟶ 130:
|-
|[[B (programming language)|B]]
|{{code|1=() [] ! ~ ++ -- + - * & / % << >> < <= > >= == != ^ <nowiki>|</nowiki> [[?:]] = =+ =- =* =/ =% =& =^ =<nowiki>|</nowiki>}}<ref>{{Cite web |title=A TUTORIAL INTRODUCTION TO THE LANGUAGE B |url=https://www.bell-labs.com/usr/dmr/www/btut.html |access-date=2024-08-03 |archive-date=2017-04-03 |archive-url=https://web.archive.org/web/20170403063756/https://www.bell-labs.com/usr/dmr/www/btut.html |url-status=dead }}</ref>
|
|{{Yes}}
Line 198 ⟶ 211:
| colspan="2" {{Yes}}, using [[Type class]]es
| {{Yes}}
|-
| [[MultiValue|mvBasic Databasic/Unibasic]]
|<code>+ - * / ^ ** : = ! & [] += -= := # < > <= >= <> >< =< #> => #< </code>
|<code>AND OR NOT EQ NE LT GT LE GE MATCH ADDS() ANDS() CATS() DIVS() EQS() GES() GTS() IFS()</code>
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{No}}
|-
| [[Pascal (programming language)|Pascal]]
Line 212 ⟶ 237:
|-
| [[Perl]]
| <code>-> ++ -- ** ! ~ \ + - . =~ !~ * / % < > <= >= == != <=> ~~ & <nowiki>|</nowiki> ^ && <nowiki>||</nowiki> '
| <code>print sort chmod chdir rand and or not xor lt gt le ge eq ne cmp x </code>
| {{Yes}}
Line 322 ⟶ 347:
== See also ==
* [[
== References ==
|