Difference between revisions of "Operator overloading"
(→routing: g) |
(wikify) |
||
(21 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{LanguageBar}} |
− | Operator overloading refers to re-defining already defined 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 == |
+ | 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>]]. | ||
+ | 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 == | ||
Almost all operators can be (re-)defined: | Almost all operators can be (re-)defined: | ||
* assignment operators | * assignment operators | ||
− | ** [[Becomes|<syntaxhighlight lang="pascal" | + | ** [[Becomes|<syntaxhighlight lang="pascal" inline>:=</syntaxhighlight>]] |
− | ** the special operator <syntaxhighlight lang="delphi" | + | ** the special operator <syntaxhighlight lang="delphi" inline>explicit</syntaxhighlight> (referring to explicit [[Typecast|typecasts]]) |
* arithmetic operators | * arithmetic operators | ||
− | ** [[Plus|<syntaxhighlight lang="pascal" | + | ** [[Plus|<syntaxhighlight lang="pascal" inline>+</syntaxhighlight>]] (only binary operation) |
− | ** [[Minus|<syntaxhighlight lang="pascal" | + | ** [[Minus|<syntaxhighlight lang="pascal" inline>-</syntaxhighlight>]] (both unary and binary) |
− | ** [[Asterisk|<syntaxhighlight lang="pascal" | + | ** [[Asterisk|<syntaxhighlight lang="pascal" inline>*</syntaxhighlight>]] |
− | ** <syntaxhighlight lang="pascal" | + | ** <syntaxhighlight lang="pascal" inline>**</syntaxhighlight> |
− | ** [[Div|<syntaxhighlight lang="pascal" | + | ** [[Div|<syntaxhighlight lang="pascal" inline>div</syntaxhighlight>]] |
− | ** [[Mod|<syntaxhighlight lang="pascal" | + | ** [[Mod|<syntaxhighlight lang="pascal" inline>mod</syntaxhighlight>]] |
− | ** [[Slash|<syntaxhighlight lang="pascal" | + | ** [[Slash|<syntaxhighlight lang="pascal" inline>/</syntaxhighlight>]] |
− | ** [[Inc and Dec|<syntaxhighlight lang="pascal" | + | ** [[Inc and Dec|<syntaxhighlight lang="pascal" inline>inc</syntaxhighlight>]] |
− | ** [[Inc and Dec|<syntaxhighlight lang="pascal" | + | ** [[Inc and Dec|<syntaxhighlight lang="pascal" inline>dec</syntaxhighlight>]] |
+ | ** (As of 2020 in the future: <syntaxhighlight lang="pascal" inline>pow</syntaxhighlight>) | ||
* comparison operators | * comparison operators | ||
− | ** [[Less than|<syntaxhighlight lang="pascal" | + | ** [[Less than|<syntaxhighlight lang="pascal" inline><</syntaxhighlight>]] |
− | ** [[Greater than|<syntaxhighlight lang="pascal" | + | ** [[Greater than|<syntaxhighlight lang="pascal" inline>></syntaxhighlight>]] |
− | ** <syntaxhighlight lang="pascal" | + | ** <syntaxhighlight lang="pascal" inline>>=</syntaxhighlight> |
− | ** <syntaxhighlight lang="pascal" | + | ** <syntaxhighlight lang="pascal" inline><=</syntaxhighlight> |
− | ** [[Equal|<syntaxhighlight lang="pascal" | + | ** [[Equal|<syntaxhighlight lang="pascal" inline>=</syntaxhighlight>]] |
− | ** [[Not equal|<syntaxhighlight lang="pascal" | + | ** [[Not equal|<syntaxhighlight lang="pascal" inline><></syntaxhighlight>]] |
− | ** [[symmetric difference|<syntaxhighlight lang="pascal" | + | ** [[symmetric difference|<syntaxhighlight lang="pascal" inline>><</syntaxhighlight>]] |
− | ** [[In|<syntaxhighlight lang="pascal" | + | ** [[In|<syntaxhighlight lang="pascal" inline>in</syntaxhighlight>]] |
* logical operators | * logical operators | ||
− | ** [[And|<syntaxhighlight lang="pascal" | + | ** [[And|<syntaxhighlight lang="pascal" inline>and</syntaxhighlight>]] |
− | ** [[Or|<syntaxhighlight lang="pascal" | + | ** [[Or|<syntaxhighlight lang="pascal" inline>or</syntaxhighlight>]] |
− | ** [[Not|<syntaxhighlight lang="pascal" | + | ** [[Not|<syntaxhighlight lang="pascal" inline>not</syntaxhighlight>]] |
− | ** [[Xor|<syntaxhighlight lang="pascal" | + | ** [[Xor|<syntaxhighlight lang="pascal" inline>xor</syntaxhighlight>]] |
− | ** [[Shl|<syntaxhighlight lang="pascal" | + | ** [[Shl|<syntaxhighlight lang="pascal" inline>shl</syntaxhighlight>]] |
− | ** [[Shr|<syntaxhighlight lang="pascal" | + | ** [[Shr|<syntaxhighlight lang="pascal" inline>shr</syntaxhighlight>]] |
− | ** (As of | + | ** (As of 2020 in the future: <syntaxhighlight lang="pascal" inline>and_then</syntaxhighlight>, <syntaxhighlight lang="pascal" inline>or_else</syntaxhighlight>) |
− | * and the special operator [[for-in loop#Declaring enumerators|<syntaxhighlight lang="delphi" | + | * and the special operator [[for-in loop#Declaring enumerators|<syntaxhighlight lang="delphi" inline>enumerator</syntaxhighlight>]]. |
− | The only operators that can not be overloaded are <syntaxhighlight lang="delphi" | + | The only operators that can not be overloaded are [[@|<syntaxhighlight lang="delphi" inline>@</syntaxhighlight> (address operator)]], [[^|<syntaxhighlight lang="pascal" inline>^</syntaxhighlight> (de-referencing operator)]], <syntaxhighlight lang="delphi" inline>as</syntaxhighlight> and [[Is|<syntaxhighlight lang="delphi" inline>is</syntaxhighlight>]]. |
− | == | + | == 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" | + | * Instead of the word <syntaxhighlight lang="pascal" inline>function</syntaxhighlight> it starts with <syntaxhighlight lang="delphi" inline>operator</syntaxhighlight>. |
− | * The function's identifier is always one of the available operators e.g. <syntaxhighlight lang="pascal" | + | * 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 (if the specific overload is allowed in a Delphi mode): |
− | * The parameter list has to name exactly one or two parameters, depending on the operator. | + | {| class="wikitable" style="margin: auto;" |
− | * A result identifier | + | |+ Delphi compatibility operator names |
+ | ! operator symbol !! symbolic name | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>:=</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>implicit</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>+</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>add</syntaxhighlight>, <syntaxhighlight lang="delphi" inline>positive</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>-</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>negative</syntaxhighlight>, <syntaxhighlight lang="delphi" inline>subtract</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>*</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>multiply</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>div</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>intDivide</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>mod</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>modulus</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>/</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>divide</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>=</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>equal</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline><></syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>notEqual</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline><</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>lessThan</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline><=</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>lessThanOrEqual</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>></syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>greaterThan</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>>=</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>greaterThanOrEqual</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>not</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>logicalNot</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>and</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>logicalAnd</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>or</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>logicalOr</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>and</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>bitwiseAnd</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>or</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>bitwiseOr</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>xor</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>bitwiseXor</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>shl</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>leftShift</syntaxhighlight> | ||
+ | |- | ||
+ | | style="text-align:center;" | <syntaxhighlight lang="pascal" inline>shr</syntaxhighlight> | ||
+ | | <syntaxhighlight lang="delphi" inline>rightShift</syntaxhighlight> | ||
+ | |} | ||
+ | : 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. | ||
+ | * 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. 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. | + | 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 53: | Line 130: | ||
Some operator overloads are not allowed: | Some operator overloads are not allowed: | ||
− | * [[User Changes 2.4.0#Overloading the assignment operator with a shortstring result|overloading <syntaxhighlight lang="pascal" | + | * [[User Changes 2.4.0#Overloading the assignment operator with a shortstring result|overloading <syntaxhighlight lang="pascal" inline>shortstring</syntaxhighlight> assignments with lengths other than <syntaxhighlight lang="pascal" inline>255</syntaxhighlight>]] |
− | * | + | * [[User Changes 3.2#Operator overload + no longer allowed for dynamic arrays|<syntaxhighlight lang="pascal" inline>+</syntaxhighlight> in conjunction with dynamic arrays]] if <syntaxhighlight lang="delphi" inline>{$modeSwitch arrayOperators+}</syntaxhighlight> (since FPC 3.2) |
− | * <syntaxhighlight lang="pascal" | + | * <syntaxhighlight lang="pascal" inline>+</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>-</syntaxhighlight> in conjunction with enumeration types |
− | == | + | == Routing == |
− | + | How an operator overload definition is chosen differs in many aspects how a function is chosen. | |
− | * The assignment operator <syntaxhighlight lang="pascal" | + | * 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" | + | * 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. 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 | + | They potentially make it harder to identify problems, since it is not necessarily obvious that an operator overload applies. |
− | == | + | == 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 | + | * [https://en.wikipedia.org/wiki/Operator_overloading article “operator overloading” in the English Wikipedia] |
+ | |||
+ | [[Category:Pascal]] | ||
+ | [[Category:FPC]] |
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
- arithmetic operators
- 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 withoperator
. - 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):
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 bitwisenot
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:
- overloading
shortstring
assignments with lengths other than255
+
in conjunction with dynamic arrays if{$modeSwitch arrayOperators+}
(since FPC 3.2)+
and-
in conjunction with enumeration types
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-stylepriority
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.