Difference between revisions of "Typecast"

From Free Pascal wiki
Jump to navigationJump to search
(update URLs, typography, legacy syntaxhighlight syntax)
(→‎conversion versus typecasting: add Str procedure as a method of converting from enumerated type to a string)
 
(2 intermediate revisions by 2 users not shown)
Line 6: Line 6:
  
 
=== implicit typecasting ===
 
=== implicit typecasting ===
If the complete range of values the source could have can be stored by the destination operand, an automatic – thus “implicit” – typecast occurs.
+
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|<syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>]] can be stored in an <syntaxhighlight lang="pascal" inline>int64</syntaxhighlight>.
 
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>.
 
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.
 
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.
Line 102: Line 102:
 
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
 
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
 
| explicit
 
| explicit
| {{Doc|package=RTL|unit=system|identifier=writestr|text=<syntaxhighlight lang="pascal" inline>system.writeStr</syntaxhighlight>}}<syntaxhighlight lang="pascal" inline>(stringVariable, enumeratedVariableOrExpression)</syntaxhighlight>
+
|
 +
* {{Doc|package=RTL|unit=system|identifier=str|text=<syntaxhighlight lang="pascal" inline>system.Str</syntaxhighlight>}}<syntaxhighlight lang="pascal" inline>(enumeratedVariableOrExpression, stringVariable)</syntaxhighlight>
 +
* {{Doc|package=RTL|unit=system|identifier=writestr|text=<syntaxhighlight lang="pascal" inline>system.writeStr</syntaxhighlight>}}<syntaxhighlight lang="pascal" inline>(stringVariable, enumeratedVariableOrExpression)</syntaxhighlight>
 
|}
 
|}
 
In other cases you manually have to perform explicit typecasts:
 
In other cases you manually have to perform explicit typecasts:
Line 175: Line 177:
 
== see also ==
 
== see also ==
 
* [https://en.wikipedia.org/wiki/Type_casting_(computer_programming) “type casting (computer programming)”] in the English Wikipedia
 
* [https://en.wikipedia.org/wiki/Type_casting_(computer_programming) “type casting (computer programming)”] in the English Wikipedia
* [https://www.freepascal.org/docs-html/ref/refse83.html § “value typecasts”] in the FreePascal Reference guide
+
* [https://www.freepascal.org/docs-html/ref/refse84.html § “value typecasts”] in the FreePascal Reference guide
* [https://www.freepascal.org/docs-html/ref/refse84.html § “variable 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/refse85.html § “<syntaxhighlight lang="pascal" inline>unaligned</syntaxhighlight> 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”)
 
* [[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