Difference between revisions of "Typecast"

From Free Pascal wiki
Jump to navigationJump to search
(→‎Type conversion: Function -> Method)
(→‎conversion versus typecasting: add Str procedure as a method of converting from enumerated type to a string)
 
(20 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Typecast}}
 
{{Typecast}}
<br/>
+
Typecasting is the concept, allowing to [[Becomes|assign]] values of [[Variable|variables]] or expressions, that do not match the variable’s [[Data type|data type]], virtually overriding [[Pascal]]’s strong typing system.
<br/>
+
 
Converting values from variables with a lower value range data type to variables with higher value range data type is done automatically (implicit).
+
== definition ==
A value is converted by a variable of higher-value data type into a variable with a data type of lower values via a cast or function call, and if the value does not fit into the variable of the target data type then data will be lost.
+
Two flavors of typecasting are recognized:
<br/>
+
 
<br>
+
=== implicit typecasting ===
=Type conversion=
+
If the complete range of values the source can be stored by the destination operand, an automatic – thus “implicit” – typecast occurs.
<br/>
+
For instance, all the values of a [[Byte|<syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>]] can be stored in an <syntaxhighlight lang="pascal" inline>int64</syntaxhighlight>.
<table border ="1" cellpadding="2" cellspacing="0">
+
Assigning a <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>’s value to a <syntaxhighlight lang="pascal" inline>int64</syntaxhighlight> works without problems, since the missing 0-bits are filled in automatically, i.e. implicitly.
  <tr bgcolor="#C0C0C0">
+
The programmer does not have to insert any additional code.
    <td>Source data type</td>
+
 
    <td>Target data type</td>
+
=== explicit typecasting ===
    <td>Type of type conversion</td>
+
If the source’s range of value does not fit into the destination operand’s range, [[FPC|the compiler]] will not compile the program, unless it is instructed to ignore this.
    <td>Method</td>
+
There are two different explicit typecasts:
  </tr>
+
 
  <tr>
+
==== value typecast ====
    <td>Integer</td>
+
A value typecast is done, by prepending a data type identifier and surrounding the expression to typecast with parentheses, like this:
    <td>Real</td>
+
<syntaxhighlight lang="pascal" inline>dataType(expression)</syntaxhighlight>.
    <td>implicit</td>
+
Extraneous bits are just cut off.
    <td></td>
+
This approach is usually used, if there is absolute certainty, the actual value of the <syntaxhighlight lang="pascal" inline>expression</syntaxhighlight> will fit into the destination.
  </tr>
+
Occasionally this effect is also used instead of a [[Mod|modulo operation]].
  <tr>
+
 
    <td>Real</td>
+
==== variable typecast ====
    <td>Integer</td>
+
A variable typecast treats a variable as if it were a different type.
    <td>explicit:<br>Cut off the fractional part<br/> Round fractional part</td>
+
Retrieving and storing the variable is done, as if it was the specified data type.
    <td><br>Trunc<br>Round</td>
+
Just as a value typecast, the data type identifier is prepended and parentheses surround, in this case, a variable identifier:
  </tr>
+
<syntaxhighlight lang="pascal" inline>dataTypeIdentifier(variableIdentifier)</syntaxhighlight>.
  <tr>
+
Unlike a value typecast, the variable typecast can occur on both sides of an assignment.
    <td>Integer</td>
+
 
    <td>String</td>
+
== conversion versus typecasting ==
    <td>explicit</td>
+
Type conversion is the ordered process of mapping the domain’s values to a co-domain, possibly triggering [[Exceptions|exceptions]] or [[runtime error|run-time errors]].
    <td>IntToStr</td>
+
This is done by properly defined [[Function|functions]].
  </tr>
+
Bare typecasts on the other hand are always with brute force.
  <tr>
+
They cut and push the bits 1:1 to the destination.
    <td>Real</td>
+
However, you may define an [[Operator overloading|operator overload]] redefining this behavior.
    <td>String</td>
+
 
    <td>explicit</td>
+
In some instances, you can convert values:
    <td>FloatToStr<br>FloatToStrF</td>
+
{| class="wikitable" style="text-align: center"
  </tr>
+
|+ conversion opportunities
  <tr>
+
! soure data type !! target data type !! type of type conversion !! method
    <td>String</td>
+
|-
    <td>Integer</td>
+
| [[Integer|<syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>]]
    <td>explicit</td>
+
| [[Real|<syntaxhighlight lang="pascal" inline>real</syntaxhighlight>]]
    <td>StrToInt</td>
+
| implicit
  </tr>
+
| assignment statement
  <tr>
+
|-
    <tr>
+
| <syntaxhighlight lang="pascal" inline>real</syntaxhighlight>
    <td>String</td>
+
| <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>
    <td>Real</td>
+
| explicit
    <td>explicit</td>
+
|
    <td>StrToFloat</td>
+
* [[Trunc|<syntaxhighlight lang="pascal" inline>trunc</syntaxhighlight>]] cuts off fractional part
  </tr>
+
* [[Round|<syntaxhighlight lang="pascal" inline>round</syntaxhighlight>]] rounds fractional part
  <tr>
+
|-
    <td>String</td>
+
| <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>
    <td>Char</td>
+
| [[String|<syntaxhighlight lang="pascal" inline>string</syntaxhighlight>]]
    <td>explicit</td>
+
| explicit
    <td>String[index]</td>
+
| {{Doc|package=RTL|unit=sysutils|identifier=inttostr|text=<syntaxhighlight lang="pascal" inline>sysUtils.intToStr</syntaxhighlight>}}
  </tr>
+
|-
  <tr>
+
| <syntaxhighlight lang="pascal" inline>real</syntaxhighlight>
    <td>Char</td>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
    <td>String</td>
+
| explicit
    <td>implicit</td>
+
|
    <td></td>
+
* {{Doc|package=RTL|unit=sysutils|identifier=floattostr|text=<syntaxhighlight lang="pascal" inline>sysUtils.floatToStr</syntaxhighlight>}}
  </tr>
+
* {{Doc|package=RTL|unit=sysutils|identifier=floattostrf|text=<syntaxhighlight lang="pascal" inline>sysUtils.floatToStrF</syntaxhighlight>}}
  <tr>
+
|-
    <td>Char/AnsiChar</td>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
    <td>Byte</td>
+
| <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>
    <td>explicit</td>
+
| explicit
    <td>Ord<br/>Byte(char-value)</td>
+
| {{Doc|package=RTL|unit=sysutils|identifier=strtoint|text=<syntaxhighlight lang="pascal" inline>sysUtils.strToInt</syntaxhighlight>}}
  </tr>
+
|-
  <tr>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
    <td>Byte</td>
+
| <syntaxhighlight lang="pascal" inline>real</syntaxhighlight>
    <td>Char/AnsiChar</td>
+
| explicit
    <td>explicit</td>
+
| {{Doc|package=RTL|unit=sysutils|identifier=strtofloat|text=<syntaxhighlight lang="pascal" inline>sysUtils.strToFloat</syntaxhighlight>}}
    <td>Chr<br/>AnsiChar(byte-value)</td>
+
|-
  </tr>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
</table>
+
| [[Char|<syntaxhighlight lang="pascal" inline>char</syntaxhighlight>]]
<br>
+
| explicit
<table border ="1" cellpadding="2" cellspacing="0">
+
| <syntaxhighlight lang="pascal" inline>stringVariable[indexExpression]</syntaxhighlight>
  <tr bgcolor="#C0C0C0">
+
|-
    <td>Source data type</td>
+
| <syntaxhighlight lang="pascal" inline>char</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>ANSIChar</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>wideChar</syntaxhighlight>
    <td>Target data type</td>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
    <td>Type of type conversion</td>
+
| implicit
    <td>Method</td>
+
| assignment statement
  </tr>
+
|-
  <tr>
+
| <syntaxhighlight lang="pascal" inline>char</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>ANSIChar</syntaxhighlight>
    <td>QWord</td>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
    <td>Byte</td>
+
| explicit
    <td>explicit</td>
+
|
    <td>Byte(QWord)</td>
+
* [[Ord|<syntaxhighlight lang="pascal" inline>ord</syntaxhighlight>]]
  </tr>
+
* <syntaxhighlight lang="pascal" inline>byte(characterVariableOrExpression)</syntaxhighlight>
  <tr>
+
|-
    <td>QWord</td>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
    <td>Word</td>
+
| <syntaxhighlight lang="pascal" inline>char</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>ANSIChar</syntaxhighlight>
    <td>explicit</td>
+
| explicit
    <td>Word(QWord)</td>
+
|
  </tr>
+
* [[Chr|<syntaxhighlight lang="pascal" inline>chr</syntaxhighlight>]]
  <tr>
+
* <syntaxhighlight lang="pascal" inline>ANSIChar(byteVariableOrExpression)</syntaxhighlight>
    <td>QWord</td>
+
|-
    <td>Cardinal</td>
+
| enumerated type
    <td>explicit</td>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
    <td>Cardinal(QWord)</td>
+
| explicit
  </tr>
+
|
  <tr>
+
* {{Doc|package=RTL|unit=system|identifier=str|text=<syntaxhighlight lang="pascal" inline>system.Str</syntaxhighlight>}}<syntaxhighlight lang="pascal" inline>(enumeratedVariableOrExpression, stringVariable)</syntaxhighlight>
    <td>QWord</td>
+
* {{Doc|package=RTL|unit=system|identifier=writestr|text=<syntaxhighlight lang="pascal" inline>system.writeStr</syntaxhighlight>}}<syntaxhighlight lang="pascal" inline>(stringVariable, enumeratedVariableOrExpression)</syntaxhighlight>
    <td>Longword</td>
+
|}
    <td>explicit</td>
+
In other cases you manually have to perform explicit typecasts:
    <td>Longword(QWord)</td>
+
{| class="wikitable" style="text-align: center"
  </tr>
+
|+ typecasting
  <tr>
+
! source data type !! target data type !! type of type conversion !! method
    <td>Longword</td>
+
|-
    <td>Byte</td>
+
| [[QWord|<syntaxhighlight lang="pascal" inline>qWord</syntaxhighlight>]]
    <td>explicit</td>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
    <td>Byte(Longword)</td>
+
| explicit
  </tr>
+
| <syntaxhighlight lang="pascal" inline>byte(qWordVariableOrExpression)</syntaxhighlight>
  <tr>
+
|-
    <td>Longword</td>
+
| <syntaxhighlight lang="pascal" inline>qWord</syntaxhighlight>
    <td>Word</td>
+
| <syntaxhighlight lang="pascal" inline>word</syntaxhighlight>
    <td>explicit</td>
+
| explicit
    <td>Word(Longword)</td>
+
| <syntaxhighlight lang="pascal" inline>word(qWordVariableOrExpression)</syntaxhighlight>
  </tr>
+
|-
  <tr>
+
| <syntaxhighlight lang="pascal" inline>qWord</syntaxhighlight>
    <td>Longword</td>
+
| [[Cardinal|<syntaxhighlight lang="pascal" inline>cardinal</syntaxhighlight>]]
    <td>Cardinal</td>
+
| explicit
    <td>implicit (simple value assignment)</td>
+
| <syntaxhighlight lang="pascal" inline>cardinal(qWordVariableOrExpression)</syntaxhighlight>
    <td></td>
+
|-
  </tr>
+
| <syntaxhighlight lang="pascal" inline>qWord</syntaxhighlight>
  <tr>
+
| <syntaxhighlight lang="pascal" inline>longWord</syntaxhighlight>
    <td>Int64</td>
+
| explicit
    <td>Byte</td>
+
| <syntaxhighlight lang="pascal" inline>longWord(qWordVariableOrExpression)</syntaxhighlight>
    <td>explicit</td>
+
|-
    <td>Byte(Int64)</td>
+
| <syntaxhighlight lang="pascal" inline>longWord</syntaxhighlight>
  </tr>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
  <tr>
+
| explicit
    <td>Int64</td>
+
| <syntaxhighlight lang="pascal" inline>byte(longWordVariableOrExpression)</syntaxhighlight>
    <td>Shortint</td>
+
|-
    <td>explicit</td>
+
| <syntaxhighlight lang="pascal" inline>longWord</syntaxhighlight>
    <td>Shortint(Int64)</td>
+
| <syntaxhighlight lang="pascal" inline>word</syntaxhighlight>
  </tr>
+
| explicit
  <tr>
+
| <syntaxhighlight lang="pascal" inline>word(longWordVariableOrExpression)</syntaxhighlight>
    <td>Comp</td>
+
|-
    <td>Byte</td>
+
| <syntaxhighlight lang="pascal" inline>longWord</syntaxhighlight>
    <td>explicit</td>
+
| <syntaxhighlight lang="pascal" inline>cardinal</syntaxhighlight>
    <td>Byte(Comp)</td>
+
| implicit
  </tr>
+
| assignment statement
  <tr>
+
|-
    <td>Comp</td>
+
| <syntaxhighlight lang="pascal" inline>int64</syntaxhighlight>
    <td>Shortint</td>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
    <td>explicit</td>
+
| explicit
    <td>Shortint(Comp)</td>
+
| <syntaxhighlight lang="pascal" inline>byte(int64variableOrExpression)</syntaxhighlight>
  </tr>
+
|-
  <tr>
+
| <syntaxhighlight lang="pascal" inline>int64</syntaxhighlight>
    <td>Comp</td>
+
| <syntaxhighlight lang="pascal" inline>shortInt</syntaxhighlight>
    <td>Real</td>
+
| explicit
    <td>explicit</td>
+
| <syntaxhighlight lang="pascal" inline>shortInt(int64variableOrExpression)</syntaxhighlight>
    <td>Real(Comp)</td>
+
|-
  </tr>
+
| [[Comp|<syntaxhighlight lang="pascal" inline>comp</syntaxhighlight>]]
</table>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
<br>
+
| explicit
<br>
+
| <syntaxhighlight lang="pascal" inline>byte(compVariableOrExpression)</syntaxhighlight>
<br>
+
|-
[[Category:Code]]{{AutoCategory}}
+
| <syntaxhighlight lang="pascal" inline>comp</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>shortInt</syntaxhighlight>
 +
| explicit
 +
| <syntaxhighlight lang="pascal" inline>shortInt(compVariableOrExpression)</syntaxhighlight>
 +
|-
 +
| <syntaxhighlight lang="pascal" inline>comp</syntaxhighlight>
 +
| <syntaxhighlight lang="pascal" inline>real</syntaxhighlight>
 +
| explicit
 +
| <syntaxhighlight lang="pascal" inline>real(compVariableOrExpression)</syntaxhighlight>
 +
|}
 +
 
 +
== caveats ==
 +
* Explicit typecasts disable range checks altogether in the complete [[LOC|line of code]].
 +
 
 +
== see also ==
 +
* [https://en.wikipedia.org/wiki/Type_casting_(computer_programming) “type casting (computer programming)”] in the English Wikipedia
 +
* [https://www.freepascal.org/docs-html/ref/refse84.html § “value typecasts”] in the FreePascal Reference guide
 +
* [https://www.freepascal.org/docs-html/ref/refse85.html § “variable typecasts”] in the FreePascal Reference guide
 +
* [https://www.freepascal.org/docs-html/ref/refse86.html § “<syntaxhighlight lang="pascal" inline>unaligned</syntaxhighlight> typecasts”] in the FreePascal Reference guide
 +
* [[Operator overloading|operator overloading]] (especially the group of assignment operators, and § “routing”)

Latest revision as of 02:44, 18 October 2023

Deutsch (de) English (en) français (fr) русский (ru)
Typecasting is the concept, allowing to assign values of variables or expressions, that do not match the variable’s data type, virtually overriding Pascal’s strong typing system.

definition

Two flavors of typecasting are recognized:

implicit typecasting

If the complete range of values the source can be stored by the destination operand, an automatic – thus “implicit” – typecast occurs. For instance, all the values of a byte can be stored in an int64. Assigning a byte’s value to a int64 works without problems, since the missing 0-bits are filled in automatically, i.e. implicitly. The programmer does not have to insert any additional code.

explicit typecasting

If the source’s range of value does not fit into the destination operand’s range, the compiler will not compile the program, unless it is instructed to ignore this. There are two different explicit typecasts:

value typecast

A value typecast is done, by prepending a data type identifier and surrounding the expression to typecast with parentheses, like this: dataType(expression). Extraneous bits are just cut off. This approach is usually used, if there is absolute certainty, the actual value of the expression will fit into the destination. Occasionally this effect is also used instead of a modulo operation.

variable typecast

A variable typecast treats a variable as if it were a different type. Retrieving and storing the variable is done, as if it was the specified data type. Just as a value typecast, the data type identifier is prepended and parentheses surround, in this case, a variable identifier: dataTypeIdentifier(variableIdentifier). Unlike a value typecast, the variable typecast can occur on both sides of an assignment.

conversion versus typecasting

Type conversion is the ordered process of mapping the domain’s values to a co-domain, possibly triggering exceptions or run-time errors. This is done by properly defined functions. Bare typecasts on the other hand are always with brute force. They cut and push the bits 1:1 to the destination. However, you may define an operator overload redefining this behavior.

In some instances, you can convert values:

conversion opportunities
soure data type target data type type of type conversion method
integer real implicit assignment statement
real integer explicit
  • trunc cuts off fractional part
  • round rounds fractional part
integer string explicit sysUtils.intToStr
real string explicit
string integer explicit sysUtils.strToInt
string real explicit sysUtils.strToFloat
string char explicit stringVariable[indexExpression]
char/ANSIChar/wideChar string implicit assignment statement
char/ANSIChar byte explicit
  • ord
  • byte(characterVariableOrExpression)
byte char/ANSIChar explicit
  • chr
  • ANSIChar(byteVariableOrExpression)
enumerated type string explicit

In other cases you manually have to perform explicit typecasts:

typecasting
source data type target data type type of type conversion method
qWord byte explicit byte(qWordVariableOrExpression)
qWord word explicit word(qWordVariableOrExpression)
qWord cardinal explicit cardinal(qWordVariableOrExpression)
qWord longWord explicit longWord(qWordVariableOrExpression)
longWord byte explicit byte(longWordVariableOrExpression)
longWord word explicit word(longWordVariableOrExpression)
longWord cardinal implicit assignment statement
int64 byte explicit byte(int64variableOrExpression)
int64 shortInt explicit shortInt(int64variableOrExpression)
comp byte explicit byte(compVariableOrExpression)
comp shortInt explicit shortInt(compVariableOrExpression)
comp real explicit real(compVariableOrExpression)

caveats

  • Explicit typecasts disable range checks altogether in the complete line of code.

see also