Difference between revisions of "Operator overloading"

From Lazarus wiki
Jump to navigationJump to search
m (→‎Definition: add link to page Identifier)
(wikify)
 
(4 intermediate revisions by one other user not shown)
Line 2: Line 2:
  
 
[[Operator]] overloading refers to re-defining already defined [[Operators|operators]] with new definitions.
 
[[Operator]] overloading refers to re-defining already defined [[Operators|operators]] with new definitions.
The term is – although imprecisely – used for operator definitions, that have not yet been defined, too.
+
The term is – although imprecisely – used for operator definitions, that have not yet been defined, too.
  
 
== Availability ==
 
== Availability ==
 
+
The [[FPC]] supports [[PXSC]]-style operator overloading, however it is not allowed in [[Mode TP|<syntaxhighlight lang="pascal" inline>{$mode TP}</syntaxhighlight>]] or [[Mode MacPas|<syntaxhighlight lang="pascal" inline>{$mode MacPas}</syntaxhighlight>]].
[[FPC]] allows operator overloading, however it is not allowed in [[Mode TP|<syntaxhighlight lang="pascal" inline>{$mode TP}</syntaxhighlight>]] or [[Mode MacPas|<syntaxhighlight lang="pascal" inline>{$mode MacPas}</syntaxhighlight>]].
+
In [[Mode Delphi|<syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight>]], operator overloading can only be done in the context of classes or advanced records (confer [http://docwiki.embarcadero.com/RADStudio/Rio/en/Operator_Overloading_(Delphi) Delphi documentation]), so the Delphi mode does not support global operator overloads as [[Mode FPC|<syntaxhighlight lang="pascal" inline>{$mode FPC}</syntaxhighlight>]] and [[Mode ObjFPC|<syntaxhighlight lang="pascal" inline>{$mode objFPC}</syntaxhighlight>]] do.
In [[Mode Delphi|<syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight>]], operator overloading can only be done in the context of classes or advanced records (confer [http://docwiki.embarcadero.com/RADStudio/Rio/en/Operator_Overloading_(Delphi) Delphi documentation]), so the Delphi mode does not support global operator overloads as [[Mode FPC|<syntaxhighlight lang="pascal" inline>{$mode FPC}</syntaxhighlight>]] and [[Mode ObjFPC|<syntaxhighlight lang="pascal" inline>{$mode objFPC}</syntaxhighlight>]] do.
 
  
 
== Overloadable operators ==
 
== Overloadable operators ==
 
 
Almost all operators can be (re-)defined:
 
Almost all operators can be (re-)defined:
 
* assignment operators
 
* assignment operators
Line 47: Line 45:
  
 
== Definition ==
 
== Definition ==
 
 
An operator is declared as if it was a [[Function|function]], with a few differences:
 
An operator is declared as if it was a [[Function|function]], with a few differences:
 
* Instead of the word <syntaxhighlight lang="pascal" inline>function</syntaxhighlight> it starts with <syntaxhighlight lang="delphi" inline>operator</syntaxhighlight>.
 
* Instead of the word <syntaxhighlight lang="pascal" inline>function</syntaxhighlight> it starts with <syntaxhighlight lang="delphi" inline>operator</syntaxhighlight>.
* The function's [[Identifier|identifier]] is always one of the available operators e.g. <syntaxhighlight lang="pascal" inline>><</syntaxhighlight>, although this would not constitute a valid identifier anywhere else. However, in <syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight> a spelled out operator name ''has'' to be used. These are allowed in other modes, too:
+
* The function's [[Identifier|identifier]] is always one of the available operators e.&#8239;g. <syntaxhighlight lang="pascal" inline>><</syntaxhighlight>, although this would not constitute a valid identifier anywhere else. However, in <syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight> a spelled out operator name ''has'' to be used. These are allowed in other modes, too (if the specific overload is allowed in a Delphi mode):
 
{| class="wikitable" style="margin: auto;"
 
{| class="wikitable" style="margin: auto;"
 
|+ Delphi compatibility operator names
 
|+ Delphi compatibility operator names
Line 56: Line 53:
 
|-
 
|-
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>:=</syntaxhighlight>
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>:=</syntaxhighlight>
| <syntaxhighlight lang="delphi" inline>assign</syntaxhighlight>
+
| <syntaxhighlight lang="delphi" inline>implicit</syntaxhighlight>
 
|-
 
|-
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>+</syntaxhighlight>
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>+</syntaxhighlight>
| <syntaxhighlight lang="delphi" inline>add</syntaxhighlight>
+
| <syntaxhighlight lang="delphi" inline>add</syntaxhighlight>, <syntaxhighlight lang="delphi" inline>positive</syntaxhighlight>
 
|-
 
|-
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>-</syntaxhighlight>
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>-</syntaxhighlight>
| <syntaxhighlight lang="delphi" inline>subtract</syntaxhighlight>
+
| <syntaxhighlight lang="delphi" inline>negative</syntaxhighlight>, <syntaxhighlight lang="delphi" inline>subtract</syntaxhighlight>
 
|-
 
|-
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>*</syntaxhighlight>
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>*</syntaxhighlight>
Line 93: Line 90:
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>>=</syntaxhighlight>
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>>=</syntaxhighlight>
 
| <syntaxhighlight lang="delphi" inline>greaterThanOrEqual</syntaxhighlight>
 
| <syntaxhighlight lang="delphi" inline>greaterThanOrEqual</syntaxhighlight>
|-
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>><</syntaxhighlight>
 
| <syntaxhighlight lang="delphi" inline>symmetricalDifference</syntaxhighlight>
 
 
|-
 
|-
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>not</syntaxhighlight>
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>not</syntaxhighlight>
Line 105: Line 99:
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>or</syntaxhighlight>
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>or</syntaxhighlight>
 
| <syntaxhighlight lang="delphi" inline>logicalOr</syntaxhighlight>
 
| <syntaxhighlight lang="delphi" inline>logicalOr</syntaxhighlight>
|-
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>not</syntaxhighlight>
 
| <syntaxhighlight lang="delphi" inline>bitwiseNot</syntaxhighlight>
 
 
|-
 
|-
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>and</syntaxhighlight>
 
| style="text-align:center;" | <syntaxhighlight lang="pascal" inline>and</syntaxhighlight>
Line 124: Line 115:
 
| <syntaxhighlight lang="delphi" inline>rightShift</syntaxhighlight>
 
| <syntaxhighlight lang="delphi" inline>rightShift</syntaxhighlight>
 
|}
 
|}
* The parameter list has to name exactly one or two parameters, depending on the operator.
+
: Note, <syntaxhighlight lang="pascal" inline>><</syntaxhighlight>, <syntaxhighlight lang="delphi" inline>**</syntaxhighlight> and the bitwise <syntaxhighlight lang="pascal" inline>not</syntaxhighlight> have no equivalent in Delphi, but the symbolic operator designation is allowed instead.
 +
* The parameter list has to name exactly one or two parameters, depending on the operator. The first formal parameter always refers, if applicable, to the operand on the left-hand side.
 
* A result identifier has to be specified in front of the colon separating the result type, but can be omitted where the special identifier <syntaxhighlight lang="pascal" inline>result</syntaxhighlight> is available.
 
* A result identifier has to be specified in front of the colon separating the result type, but can be omitted where the special identifier <syntaxhighlight lang="pascal" inline>result</syntaxhighlight> is available.
* Note, the concept of “default parameters,” that means a [[Routine#default values|default value]] for a parameter, only applies to “real” functions. When an operator is used, e.g. in an [[expression]], you can not just skip naming an operand, hoping the compiler will insert “something”. Therefore, the concept of default parameter values does not apply to operator overloads.
+
* Comparisons ''have'' to yield a [[Boolean|<syntaxhighlight lang="pascal" inline>Boolean</syntaxhighlight> value]].
 +
* The “source” and “target” types of assignment operator overloads must differ.
 +
* Note, the concept of “default parameters,” that means a [[Routine#default values|default value]] for a parameter, only applies to “real” functions. When an operator is used, e.&#x202F;g. in an [[expression]], you can not just skip naming an operand, hoping the compiler will insert “something”. Therefore, the concept of [[Default parameter|default parameter]] values does not apply to operator overloads.
  
The following shows a valid operator declaration (in <syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight> the <syntaxhighlight lang="pascal" inline>:=</syntaxhighlight> has to be replaced by <syntaxhighlight lang="delphi" inline>assign</syntaxhighlight> and the declaration may only appear in the context of class or advanced record definitions).
+
The following shows a valid operator declaration (in <syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight> the <syntaxhighlight lang="pascal" inline>:=</syntaxhighlight> has to be replaced by <syntaxhighlight lang="delphi" inline>implicit</syntaxhighlight> and the declaration may only appear in the context of class or advanced record definitions).
 
<syntaxhighlight lang="delphi">
 
<syntaxhighlight lang="delphi">
 
operator := (x: myNewType) y: someOtherType;
 
operator := (x: myNewType) y: someOtherType;
Line 141: Line 135:
  
 
== Routing ==
 
== Routing ==
 
 
How an operator overload definition is chosen differs in many aspects how a function is chosen.
 
How an operator overload definition is chosen differs in many aspects how a function is chosen.
 
* The assignment operator <syntaxhighlight lang="pascal" inline>:=</syntaxhighlight> is used for ''implicit'' typecasts. Everywhere, where a value has to be stored in memory, an implicit typecast may occur. Note, that ''calling'' a [[Routine|routine]] requires storing its parameters in memory, too, thus the parameter list might become subject of implicit typecasts as well, even though on the surface they are not part of an assignment statement.
 
* The assignment operator <syntaxhighlight lang="pascal" inline>:=</syntaxhighlight> is used for ''implicit'' typecasts. Everywhere, where a value has to be stored in memory, an implicit typecast may occur. Note, that ''calling'' a [[Routine|routine]] requires storing its parameters in memory, too, thus the parameter list might become subject of implicit typecasts as well, even though on the surface they are not part of an assignment statement.
* Unlike regular function overloads, assignment operator overloads are chosen by their result type.
+
* Unlike regular function overloads, assignment operator overloads are chosen ''by their result type''.
 
* Operator overloads can not be chosen explicitly by their [[Scope|scope]] they are defined in. Something like <syntaxhighlight lang="pascal" inline>unitDefiningOverloads.+</syntaxhighlight> is not possible. The last operator definition ''always'' wins and this can not be changed.
 
* Operator overloads can not be chosen explicitly by their [[Scope|scope]] they are defined in. Something like <syntaxhighlight lang="pascal" inline>unitDefiningOverloads.+</syntaxhighlight> is not possible. The last operator definition ''always'' wins and this can not be changed.
* Operator precedence remains as usual for all operator symbols. When defining operators for a new custom type from scratch, the <syntaxhighlight lang="pascal" inline>*</syntaxhighlight> will still bind stronger than the <syntaxhighlight lang="pascal" inline>+</syntaxhighlight>. The operator precedence system can not be altered.
+
* Operator precedence remains as usual for all operator symbols. When defining operators for a new custom type from scratch, the <syntaxhighlight lang="pascal" inline>*</syntaxhighlight> will still bind stronger than the <syntaxhighlight lang="pascal" inline>+</syntaxhighlight>. The operator precedence system can not be altered. FPC does not support [[PXSC]]-style <syntaxhighlight lang="pascal" inline>priority</syntaxhighlight> clauses.
  
 
Operator overloads should be used with caution.
 
Operator overloads should be used with caution.
They potentially make it harder to identify problems, since it is not necessarily obvious, that an operator overload applies.
+
They potentially make it harder to identify problems, since it is not necessarily obvious that an operator overload applies.
  
 
== See also ==
 
== See also ==
 
 
* [https://www.freepascal.org/docs-html/ref/refch15.html chapter “Operator overloading” in the “Free Pascal Reference Guide”]
 
* [https://www.freepascal.org/docs-html/ref/refch15.html chapter “Operator overloading” in the “Free Pascal Reference Guide”]
 
* [https://en.wikipedia.org/wiki/Operator_overloading article “operator overloading” in the English Wikipedia]
 
* [https://en.wikipedia.org/wiki/Operator_overloading article “operator overloading” in the English Wikipedia]

Latest revision as of 08:05, 28 February 2024

English (en)

Operator overloading refers to re-defining already defined operators with new definitions. The term is – although imprecisely – used for operator definitions, that have not yet been defined, too.

Availability

The FPC supports PXSC-style operator overloading, however it is not allowed in {$mode TP} or {$mode MacPas}. In {$mode Delphi}, operator overloading can only be done in the context of classes or advanced records (confer Delphi documentation), so the Delphi mode does not support global operator overloads as {$mode FPC} and {$mode objFPC} do.

Overloadable operators

Almost all operators can be (re-)defined:

  • assignment operators
    • :=
    • the special operator explicit (referring to explicit typecasts)
  • arithmetic operators
    • + (only binary operation)
    • - (both unary and binary)
    • *
    • **
    • div
    • mod
    • /
    • inc
    • dec
    • (As of 2020 in the future: pow)
  • comparison operators
  • logical operators
  • and the special operator enumerator.

The only operators that can not be overloaded are @ (address operator), ^ (de-referencing operator), as and is.

Definition

An operator is declared as if it was a function, with a few differences:

  • Instead of the word function it starts with operator.
  • The function's identifier is always one of the available operators e. g. ><, although this would not constitute a valid identifier anywhere else. However, in {$mode Delphi} a spelled out operator name has to be used. These are allowed in other modes, too (if the specific overload is allowed in a Delphi mode):
Delphi compatibility operator names
operator symbol symbolic name
:= implicit
+ add, positive
- negative, subtract
* multiply
div intDivide
mod modulus
/ divide
= equal
<> notEqual
< lessThan
<= lessThanOrEqual
> greaterThan
>= greaterThanOrEqual
not logicalNot
and logicalAnd
or logicalOr
and bitwiseAnd
or bitwiseOr
xor bitwiseXor
shl leftShift
shr rightShift
Note, ><, ** and the bitwise not have no equivalent in Delphi, but the symbolic operator designation is allowed instead.
  • The parameter list has to name exactly one or two parameters, depending on the operator. The first formal parameter always refers, if applicable, to the operand on the left-hand side.
  • A result identifier has to be specified in front of the colon separating the result type, but can be omitted where the special identifier result is available.
  • Comparisons have to yield a Boolean value.
  • The “source” and “target” types of assignment operator overloads must differ.
  • Note, the concept of “default parameters,” that means a default value for a parameter, only applies to “real” functions. When an operator is used, e. g. in an expression, you can not just skip naming an operand, hoping the compiler will insert “something”. Therefore, the concept of default parameter values does not apply to operator overloads.

The following shows a valid operator declaration (in {$mode Delphi} the := has to be replaced by implicit and the declaration may only appear in the context of class or advanced record definitions).

operator := (x: myNewType) y: someOtherType;

Operators are defined the same way as any other function, by following the signature with a block.

Some operator overloads are not allowed:

Routing

How an operator overload definition is chosen differs in many aspects how a function is chosen.

  • The assignment operator := is used for implicit typecasts. Everywhere, where a value has to be stored in memory, an implicit typecast may occur. Note, that calling a routine requires storing its parameters in memory, too, thus the parameter list might become subject of implicit typecasts as well, even though on the surface they are not part of an assignment statement.
  • Unlike regular function overloads, assignment operator overloads are chosen by their result type.
  • Operator overloads can not be chosen explicitly by their scope they are defined in. Something like unitDefiningOverloads.+ is not possible. The last operator definition always wins and this can not be changed.
  • Operator precedence remains as usual for all operator symbols. When defining operators for a new custom type from scratch, the * will still bind stronger than the +. The operator precedence system can not be altered. FPC does not support PXSC-style priority clauses.

Operator overloads should be used with caution. They potentially make it harder to identify problems, since it is not necessarily obvious that an operator overload applies.

See also