Content deleted Content added
m WP:CHECKWIKI error fix. Section heading problem. Violates WP:MOSHEAD. |
Reverting vandalism. Tag: nowiki added |
||
Line 1:
{{refimprove|date=January 2012}}
[[Programming languages]] typically support a set of '''operators''': constructs which behave generally like functions, but which differ syntactically or semantically from usual functions. Common simple examples include arithmetic (addition with <code>+</code>), comparison (with <code>></code>), and logical operations (such as <code>AND</code> or <code>&&</code>). More involved examples include assignment (usually <code>=</code> or <code>:=</code>), [[Field (computer science)|field]] access in a record or object (usually <code>.</code>), and the [[scope resolution operator]] (often <code>::</code>). Languages usually define a set of built-in operators, and in some cases allow user-defined operators.
==Syntax==
[[Syntax (programming languages)|Syntactically]] operators usually contrast to [[Function (computer science)|functions]]. In most languages, functions may be seen as a special form of prefix operator with fixed [[Order of operations|precedence]] level and associativity, often with compulsory [[Bracket#Parentheses .28 .29|parentheses]] e.g. <code>Func(a)</code> (or <code>(Func a)</code> in [[Lisp (programming language)|LISP]]). Most languages support programmer-defined functions, but cannot really claim to support programmer-defined operators, unless they have more than prefix notation and more than a single precedence level. Semantically operators can be seen as special form of function with different calling notation and a limited number of parameters (usually 1 or 2).
The position of the operator with respect to its operands may be [[Polish notation|prefix]], [[infix notation|infix]] or [[postfix notation|postfix]], and the syntax of an [[expression (computer science)|expression]] involving an operator depends on its [[arity]] (number of [[operand]]s), precedence, and (if applicable), [[Operator associativity|associativity]]. Most programming languages support [[binary operation|binary operators]] and a few [[unary operation|unary operators]], with a few supporting more operands, such as the [[?:]] operator in C, which is ternary. There are prefix unary operators, such as unary minus <code>-x</code>, and postfix unary operators, such as [[post-increment]] <code>x++</code>; and binary operations are infix, such as <code>x + y</code> or <code>x = y</code>. Infix operations of higher arity require additional symbols, such as the [[ternary operator]] ?: in C, written as <code>a ? b : c</code> – indeed, this is the only common example, it is often referred to as ''the'' ternary operator. Prefix and postfix operations can support any desired arity, however, such as <code>1 2 3 4 +</code>.
Occasionally<ref>[http://reference.wolfram.com/mathematica/tutorial/OperatorInputForms.html#11350 Mathematica reference]</ref><ref>[http://maxima.sourceforge.net/docs/manual/en/maxima_7.html#SEC36 Maxima reference]</ref> parts of a language may be described as "matchfix" or "circumfix"<ref>[http://mythryl.org/my-Prefix__Postfix_and_Circumfix_Operators.html Prefix, Postfix and Circumfix Operators in Mythryl]</ref><ref>[http://doc.perl6.org/language/operators#___top Common Perl 6 infixes, prefixes, postfixes, and more!]</ref> operators, either to simplify the language's description or implementation. A circumfix operator consists of two or more parts which enclose its operands. Circumfix operators have the highest precedence, with their contents being evaluated and the resulting value used in the surrounding expression. The most familiar circumfix operator are the parentheses mentioned above, used to indicate which parts of an expression are to be evaluated before others. Another example from physics is the [[Inner product space|inner product]] notation of Dirac's [[bra–ket notation]]. Circumfix operators are especially useful to denote operations that involve many or varying numbers of operands.
The specification of a language will specify the syntax the operators it supports, while languages such as [[Prolog]] that support programmer-defined operators require that the syntax be defined by the programmer.
==Semantics==
The semantics of operators particularly depends on value, evaluation strategy, and argument passing mode (such as boolean short-circuiting). Simply, an [[Expression (computer science)|expression]] involving an operator is evaluated in some way, and the resulting [[Value (computer science)|value]] may be just a value (an r-value), or may be an object allowing assignment (an l-value).
In simple cases this is identical to usual function calls; for example, addition <code>x + y</code> is generally equivalent to a function call <code>add(x, y)</code> and less-than comparison <code>x < y</code> to <code>lt(x, y)</code>, meaning that the arguments are evaluated in their usual way, then some function is evaluated and the result is returned as a value. However, the semantics can be significantly different. For example, in assignment <code>a = b</code> the target <code>a</code> is not evaluated, but instead its ''___location'' (address) is used to store the value of <code>b</code> – corresponding to [[call-by-reference]] semantics. Further, an assignment may be a statement (no value), or may be an expression (value), with the value itself either an r-value (just a value) or an l-value (able to be assigned to). As another example, the [[scope resolution operator]] :: and the element access operator . (as in <code>Foo::Bar</code> or <code>a.b</code>) operate not on values, but on ''names'', essentially [[call-by-name]] semantics, and their value is a name.
Use of l-values as operator operands is particularly notable in unary [[increment and decrement operators]]. In C, for instance, the following statement is legal and well-defined, and depends on the fact that array indexing returns an l-value:
<source lang=c>
x = ++a[i];
</source>
An important use is when a left-associative binary operator modifies its left argument (or produces a side effect) and then evaluates to that argument as an l-value.{{efn|Conversely a right-associative operator with its right argument, though this is rarer.}} This allows a sequence of operators all affecting the original argument, allowing a [[fluent interface]], similar to [[method cascading]]. A common example is the <code><<</code> operator in the C++ <code>[[iostream]]</code> library, which allows fluent output, as follows:
<source lang=cpp>
cout << "Hello" << " " << "world!" << endl;
</source>
==User-defined operators==
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. [[F Sharp (programming language)|F#]], [[OCaml]], [[Haskell programming language|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]]).
Most languages have a built-in set of operators, but do not allow user-defined operators, as this significantly complicates parsing.{{efn|Introducing a new operator changes the [[lexical specification]] of the language, which changes the [[lexical analysis]]. The arity and precedence of the operator is then part of the phrase syntax of the language, which changes the phrase-level analysis. For example, adding an operator <code>@</code> requires lexing and tokenizing this character, and the phrase structure (syntax tree) depends on the arity and precedence of this operator.}} Many languages only allow operators to be used for built-in types, but others allow existing operators to be used for user-defined types; this is known as [[operator overloading]]. Some languages allow new operators to be defined, however, either at compile time or at run time. This may involve meta-programming (specifying the operators in a separate language), or within the language itself. Definition of new operators, particularly runtime definition, often makes correct [[static analysis]] of programs 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]].
==Examples==
{{category see also|Operators (programming)}}
Common examples that differ syntactically are mathematical [[arithmetic operation]]s, e.g. ">" for "[[inequality (mathematics)|greater than]]", with names often outside the language's set of [[identifier (computer science)|identifiers]] for functions, and called with a syntax different from the language's syntax for calling functions. As a function, "greater than" would generally be named by an identifier, such as <code>gt</code> or <code>greater_than</code> and called as a function, as <code>gt(x, y)</code>. Instead, the operation uses the special character <code>></code> (which is tokenized separately during [[lexical analysis]]), and infix notation, as <code>x > y</code>.
Common examples that differ semantically (by argument passing mode) are boolean operations, which frequently feature [[short-circuit evaluation]]: e.g. a short-circuiting conjunction (X AND Y) that only evaluates later arguments if earlier ones are not false, in a language with strict call-by-value functions. This behaves instead similarly to if/then/else.
Less common operators include:
* [[Comma operator]]: <code>e, f</code>
* [[Dereference operator]]: <code>*p</code> and address-of operator: <code>&x</code>
* [[?:]] or ternary operator: <code>number = spell_out_numbers ? "forty-two" : 42</code>
** [[Elvis operator]]: <code>x ?: y</code>
* [[Null coalescing operator]]: <code>x ?? y</code>
* [[Spaceship operator]] (for [[three-way comparison]]): <code>x <=> y</code>
==Compilation==
A compiler can implement operators and functions with [[Subroutine|subroutine calls]] or with [[Inline expansion|inline code]]. Some built-in operators supported by a language have a direct mapping to a small number of [[Machine code|instructions]] commonly found on [[central processing units]], though others (''e.g.'' '+' used to express [[string concatenation]]) may have complicated implementations.
== Operator overloading ==
{{main|Operator overloading}}
In some programming languages an operator may be ''ad-hoc polymorphic'', that is, have definitions for more than one kind of data, (such as in [[Java (programming language)|Java]] where the <tt>+</tt> operator is used both for the addition of numbers and for the concatenation of strings). Such an operator is said to be ''overloaded''. In languages that support operator overloading by the programmer (such as [[C++]]) but have a limited set of operators, operator overloading is often used to define customized uses for operators.
In the example <code>IF ORDER_DATE > "12/31/2011" AND ORDER_DATE < "01/01/2013" THEN CONTINUE ELSE STOP</code>, the operators are: ">" (greater than), "AND" and "<" (less than).
== Operand coercion ==
{{further|Type conversion}}
Some languages also allow for the operands of an operator to be implicitly converted, or ''[[Type conversion#Implicit type conversion|coerced]]'', to suitable data types for the operation to occur. For example, in [[Perl]] coercion rules lead into <code>12 + "3.14"</code> producing the result of <code>15.14</code>. The text <code>"3.14"</code> is converted to the number 3.14 before addition can take place. Further, <code>12</code> is an integer and <code>3.14</code> is either a floating or fixed-point number (a number that has a decimal place in it) so the integer is then converted to a floating point or fixed-point number respectively.
[[JavaScript]] follows opposite rules—finding the same expression above, it will convert the integer <code>12</code> into a string <code>"12"</code>, then concatenate the two operands to form <code>"123.14"</code>.
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.
== Operator features in programming languages ==
The following table shows the operator features in several programming languages:
{| class="sortable wikitable"
|-
!Programming language
!Nonalphanumeric operator symbols
!Alphanumeric operator symbols
!Prefix
!Infix
!Postfix
!Precedence
!Associativity
!Overloading
!Programmer-defined overloading
!Programmer-defined operator symbols
|-
| [[ALGOL 68]]
| {{mono|1=+* ** * / % %* %× - + < <= >= > = /= & -:= +:= *:= /:= %:= %*:= +=: :=: :/=:}}
(All operators have '''bold''' Alphanumeric equivalents, c.f. next column. Some have non [[ASCII]] equivalents, c.f. below.)
{{mono|1=¬ +× ⊥ ↑ ↓ ⌊ ⌈ × ÷ ÷× ÷* □ ≤ ≥ ≠ ∧ ∨ ×:= ÷:= ÷×:= ÷*:= %×:= :≠:}}
| {{mono|'''not''' '''abs''' '''arg''' '''bin''' '''entier''' '''leng''' '''level''' '''odd''' '''repr''' '''round''' '''shorten''' '''i''' '''shl''' '''shr''' '''up''' '''down''' '''lwb''' '''upb''' '''lt''' '''le''' '''ge''' '''gt''' '''eq''' '''ne''' '''and''' '''or''' '''over''' '''mod''' '''elem''' '''minusab''' '''plusab''' '''timesab''' '''divab''' '''overab''' '''modab''' '''plusto''' '''is''' '''{{Not a typo|isnt}}'''}}
| {{Yes}}
| {{Yes}}
| {{No}}
| {{Yes}} <small>(prefix operators always have priority 10)</small>
| Infix operators are left associative, prefix operators are right associative
| {{Yes}}
| {{Yes}}
| {{Yes}}
|-
| [[APL (programming language)|APL]]
| {{mono|1=+ - × ÷ ⌈ ⌊ * ⍟ | ! ○ ~ ∨ ∧ ⍱ ⍲ < ≤ = ≥ > ≠ . @ ≡ ≢ ⍴ , ⍪ ⍳ ↑ ↓ ? ⍒ ⍋ ⍉ ⌽ ⊖ ∊ ⊥ ⊤ ⍎ ⍕ ⌹ ⊂ ⊃ ∪ ∩ ⍷ ⌷ ∘ → ← / ⌿ \ ⍀ ¨ ⍣ & ⍨ ⌶ ⊆ ⊣ ⊢ ⍠ ⍤ ⌸ ⌺ ⍸}}
| bgcolor="yellow" | Alphanumeric symbols need a ⎕ before the keyword
| {{Yes}} <small>(first-order functions only)</small>
| {{Yes}}
| {{Yes}} <small>(higher-order functions only)</small>
| Higher-order functions precede first-order functions
| Higher-order functions are left associative, first-order functions are right associative
| {{Yes}}
| {{Yes}}
| {{Yes}} <small>(alphanumeric only)</small>
|-
| [[C (programming language)|C]]
| rowspan="3" | {{mono|1=() [] -> . ! ~ ++ -- + - * & / % << >> < <= > >= == != ^ <nowiki>|</nowiki> && <nowiki>||</nowiki> [[?:]] = += -= *= /= %= &= ^= |= <<= >>=}}
| {{mono|1=[[sizeof]]}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{No}}
| {{No}}
|-
| [[C++]] ([[Operators in C and C++|more]])
| {{mono|1=[[sizeof]] [[typeid]] [[new (C++)|new]] [[delete (C++)|delete]] [[Exception handling|throw]] [[decltype]] [[static_cast]] [[dynamic cast]] [[reinterpret_cast]] [[const_cast]]}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{No}}
|-
| [[Java (programming language)|Java]]
| [[Java syntax#Instantiation|new]] [[instanceof]]
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{No}}
|-
| [[Eiffel (programming language)|Eiffel]]
| {{mono|1=[] + - * / // = /= }}
| {{mono|1=not and or implies "and then" "or else" }}
| {{Yes}}
| {{Yes}}
| {{No}}
| {{Yes}}
| {{Yes}}
| {{No}}
| {{Yes}}
| {{Yes}}
|-
| [[Haskell (programming language)|Haskell]]
| {{mono|1=+ - * / ^ ^^ ** == /= > < >= <= && || >>= >> $ $! . ++ !! :}} <small>Many more in common libraries</small>
| bgcolor="yellow" | The function's name must be put into backticks
| {{Yes}}
| {{Yes}}
| {{No}}
| {{Yes}}
| {{Yes}}
| colspan="2" {{Yes}}, using [[Type class]]es
| {{Yes}}
|-
| [[Pascal (programming language)|Pascal]]
| {{mono|1=* / + - = < > <> <= >= :=}}
| [[Negation#Programming|not]] [[Integer division#Division of integers|div]] [[Modulo operation|mod]] [[Logical conjunction|and]] [[Logical disjunction|or]] in
| {{Yes}}
| {{Yes}}
| {{No}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{No}}
| {{No}}
|-
| [[Perl]]
| {{mono|1=-> ++ -- ** ! ~ \ + - . =~ !~ * / % < > <= >= == != <=> ~~ & | ^ && || ' ''{{Not a typo| // .. ... ?: = += -= *= , =>}} }}
| {{mono|1=print sort chmod chdir rand and or not xor lt gt le ge eq ne cmp x }}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{No}}
|-
| [[Perl 6]]
| {{mono|1=++ -- ** ! ~ ~~ * / + - . < > <= >= == != <=> & | ^ && || //}} <ref>{{cite web |url=https://docs.perl6.org/language/operators |title=Operators}}</ref>
| {{mono|1=print sort chmod chdir rand and or not xor lt gt le ge eq ne leg cmp x xx}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}<ref>{{cite web |url=https://docs.perl6.org/language/functions#Defining_Operators |title=Defining Operators}}</ref>
|-
| [[PHP]]
| {{mono|1=[] ** ++ -- ~ @!<ref>{{cite web|url=http://php.net/manual/en/language.operators.errorcontrol.php|title=Error Controll Operators}}</ref> * / % + - . << >> < <= > >= == != === !== <> [[Spaceship operator|<=>]] & ^ <nowiki>|</nowiki> && <nowiki>||</nowiki> [[Null coalescing operator|??]] [[?:]] = += -= *= **= /= .= %= &= <nowiki>|=</nowiki> ^= <<= >>= }}
| {{mono|1=clone new unset print echo isset [[instanceof]] [[Logical conjunction|and]] [[Logical disjunction|or]] [[Exclusive or|xor]]}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{No}}
| {{No}}
| {{No}}
|-
| [[PL/I]]
| {{mono|1=( ) -> + - * / ** > ¬> >= = ¬= <= < ¬< ¬ <nowiki>&</nowiki> <nowiki>|</nowiki> <nowiki>||</nowiki>}}
|
| {{Yes}}
| {{Yes}}
| {{No}}
| {{Yes}}
| {{Yes}}
| {{No}}
| {{No}}
| {{No}}
|-
| [[Prolog]]
| {{mono|1=:- ?- ; , . =.. = \= < =< >= > == \== - + / *}}
| {{mono|1=spy nospy not is mod}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{No}}
| {{No}}
| {{Yes}}
|-
| [[Seed7]]
| {{mono|1={} [] -> ** ! + - * / << >> & >< <nowiki>|</nowiki> = <> > >= < <= <& := +:= -:= *:= /:= <<:= >>:= &:= @:=}}
| {{mono|1=conv varConv parse [[Complex conjugate|conj]] [[Integer division#Division of integers|div]] [[Remainder|rem]] [[Modulo operation|mdiv]] [[Modulo operation|mod]] times mult in [[Negation#Programming|not]] [[Logical conjunction|and]] [[Logical disjunction|or]] digits lpad rpad lpad0}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
|-
| [[Smalltalk]]
| (yes - Up to two characters<ref name="BinaryMessages">{{cite web|first=Adele|last=Goldberg|url=http://stephane.ducasse.free.fr/FreeBooks/BlueBook/Bluebook.pdf|title=Smalltalk-80: The Language and its Implementation, p. 27, ISBN 0-201-11371-6}}</ref>)
| bgcolor="yellow" | Alphanumeric symbols need a colon after the keyword
| {{No}}
| {{Yes}}
| {{Yes}}
| {{No}}
| {{No}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
|-
| [[Swift (programming language)|Swift]]
| Any Unicode symbol string except {{mono|1=.}}, including {{mono|1=! ~ + - * / % =+ =- =* =/ =% &+ &- &* =&+ =&- =&* && <nowiki>||</nowiki> << >> & <nowiki>|</nowiki> ^ == != < <= > >= ?? ... ..<}} in standard library
| {{mono|1=is as as?}}
| {{Yes}}
| {{Yes}}
| {{Yes}}
| {{Yes}} <small>(defined as partial order in precedence groups)</small>
| {{Yes}} <small>(defined as part of precedence groups)</small>
| {{Yes}}
| {{Yes}}
| {{Yes}}
|}
==See also==
*[[Relational operator]]
==Notes==
{{notelist}}
==References==
{{reflist}}
[[Category:Operators (programming)| ]]
[[Category:Programming constructs]]
|