Typecast: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Typecast
| Old | New | Line | |
|---|---|---|---|
| 1 | + | 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. | |
| 2 | + | ||
| 3 | + | ## definition | |
| 4 | + | ||
| 5 | + | Two flavors of typecasting are recognized: | |
| 6 | + | ||
| 7 | + | ### implicit typecasting | |
| 8 | + | ||
| 9 | + | 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|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. | |
| 10 | + | ||
| 11 | + | ### explicit typecasting | |
| 12 | + | ||
| 13 | + | 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. There are two different explicit typecasts: | |
| 14 | + | ||
| 15 | + | #### value typecast | |
| 16 | + | ||
| 17 | + | 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 [[Mod|modulo operation]]. | |
| 18 | + | ||
| 19 | + | #### variable typecast | |
| 20 | + | ||
| 21 | + | 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. | |
| 22 | + | ||
| 23 | + | ## conversion versus typecasting | |
| 24 | + | ||
| 25 | + | 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]]. This is done by properly defined [[Function|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 overloading|operator overload]] redefining this behavior. | |
| 26 | + | ||
| 27 | + | In some instances, you can convert values: | |
| 28 | + | ||
| 29 | + | conversion opportunities | |
| 30 | + | ||
| 31 | + | **soure data type** | |
| 32 | + | ||
| 33 | + | **target data type** | |
| 34 | + | ||
| 35 | + | **type of type conversion** | |
| 36 | + | ||
| 37 | + | **method** | |
| 38 | + | ||
| 39 | + | [[Integer|integer]] | |
| 40 | + | ||
| 41 | + | [[Real|real]] | |
| 42 | + | ||
| 43 | + | implicit | |
| 44 | + | ||
| 45 | + | assignment statement | |
| 46 | + | ||
| 47 | + | `real` | |
| 48 | + | ||
| 49 | + | `integer` | |
| 50 | + | ||
| 51 | + | explicit | |
| 52 | + | ||
| 53 | + | - [[Trunc|trunc]] cuts off fractional part | |
| 54 | + | - [[Round|round]] rounds fractional part | |
| 55 | + | ||
| 56 | + | `integer` | |
| 57 | + | ||
| 58 | + | [[String|string]] | |
| 59 | + | ||
| 60 | + | explicit | |
| 61 | + | ||
| 62 | + | [`sysUtils.intToStr`](https://www.freepascal.org/docs-html/rtl/sysutils/inttostr.html) | |
| 63 | + | ||
| 64 | + | `real` | |
| 65 | + | ||
| 66 | + | `string` | |
| 67 | + | ||
| 68 | + | explicit | |
| 69 | + | ||
| 70 | + | - [`sysUtils.floatToStr`](https://www.freepascal.org/docs-html/rtl/sysutils/floattostr.html) | |
| 71 | + | - [`sysUtils.floatToStrF`](https://www.freepascal.org/docs-html/rtl/sysutils/floattostrf.html) | |
| 72 | + | ||
| 73 | + | `string` | |
| 74 | + | ||
| 75 | + | `integer` | |
| 76 | + | ||
| 77 | + | explicit | |
| 78 | + | ||
| 79 | + | [`sysUtils.strToInt`](https://www.freepascal.org/docs-html/rtl/sysutils/strtoint.html) | |
| 80 | + | ||
| 81 | + | `string` | |
| 82 | + | ||
| 83 | + | `real` | |
| 84 | + | ||
| 85 | + | explicit | |
| 86 | + | ||
| 87 | + | [`sysUtils.strToFloat`](https://www.freepascal.org/docs-html/rtl/sysutils/strtofloat.html) | |
| 88 | + | ||
| 89 | + | `string` | |
| 90 | + | ||
| 91 | + | [[Char|char]] | |
| 92 | + | ||
| 93 | + | explicit | |
| 94 | + | ||
| 95 | + | `stringVariable[indexExpression]` | |
| 96 | + | ||
| 97 | + | `char`/`ANSIChar`/`wideChar` | |
| 98 | + | ||
| 99 | + | `string` | |
| 100 | + | ||
| 101 | + | implicit | |
| 102 | + | ||
| 103 | + | assignment statement | |
| 104 | + | ||
| 105 | + | `char`/`ANSIChar` | |
| 106 | + | ||
| 107 | + | `byte` | |
| 108 | + | ||
| 109 | + | explicit | |
| 110 | + | ||
| 111 | + | - [[Ord|ord]] | |
| 112 | + | - `byte(characterVariableOrExpression)` | |
| 113 | + | ||
| 114 | + | `byte` | |
| 115 | + | ||
| 116 | + | `char`/`ANSIChar` | |
| 117 | + | ||
| 118 | + | explicit | |
| 119 | + | ||
| 120 | + | - [[Chr|chr]] | |
| 121 | + | - `ANSIChar(byteVariableOrExpression)` | |
| 122 | + | ||
| 123 | + | enumerated type | |
| 124 | + | ||
| 125 | + | `string` | |
| 126 | + | ||
| 127 | + | explicit | |
| 128 | + | ||
| 129 | + | - [`system.Str`](https://www.freepascal.org/docs-html/rtl/system/str.html)`(enumeratedVariableOrExpression, stringVariable)` | |
| 130 | + | - [`system.writeStr`](https://www.freepascal.org/docs-html/rtl/system/writestr.html)`(stringVariable, enumeratedVariableOrExpression)` | |
| 131 | + | ||
| 132 | + | In other cases you manually have to perform explicit typecasts: | |
| 133 | + | ||
| 134 | + | | source data type | target data type | type of type conversion | method | | |
| 135 | + | |----|----|----|----| | |
| 136 | + | | [[QWord|qWord]] | `byte` | explicit | `byte(qWordVariableOrExpression)` | | |
| 137 | + | | `qWord` | `word` | explicit | `word(qWordVariableOrExpression)` | | |
| 138 | + | | `qWord` | [[Cardinal|cardinal]] | explicit | `cardinal(qWordVariableOrExpression)` | | |
| 139 | + | | `qWord` | `longWord` | explicit | `longWord(qWordVariableOrExpression)` | | |
| 140 | + | | `longWord` | `byte` | explicit | `byte(longWordVariableOrExpression)` | | |
| 141 | + | | `longWord` | `word` | explicit | `word(longWordVariableOrExpression)` | | |
| 142 | + | | `longWord` | `cardinal` | implicit | assignment statement | | |
| 143 | + | | `int64` | `byte` | explicit | `byte(int64variableOrExpression)` | | |
| 144 | + | | `int64` | `shortInt` | explicit | `shortInt(int64variableOrExpression)` | | |
| 145 | + | | [[Comp|comp]] | `byte` | explicit | `byte(compVariableOrExpression)` | | |
| 146 | + | | `comp` | `shortInt` | explicit | `shortInt(compVariableOrExpression)` | | |
| 147 | + | | `comp` | `real` | explicit | `real(compVariableOrExpression)` | | |
| 148 | + | ||
| 149 | + | typecasting | |
| 150 | + | ||
| 151 | + | ## caveats | |
| 152 | + | ||
| 153 | + | - Explicit typecasts disable range checks altogether in the complete [[LOC|line of code]]. | |
| 154 | + | ||
| 155 | + | ## see also | |
| 156 | + | ||
| 157 | + | - [“type casting (computer programming)”](https://en.wikipedia.org/wiki/Type_casting_(computer_programming)) in the English Wikipedia | |
| 158 | + | - [§ “value typecasts”](https://www.freepascal.org/docs-html/ref/refse84.html) in the FreePascal Reference guide | |
| 159 | + | - [§ “variable typecasts”](https://www.freepascal.org/docs-html/ref/refse85.html) in the FreePascal Reference guide | |
| 160 | + | - [§ “`unaligned` typecasts”](https://www.freepascal.org/docs-html/ref/refse86.html) in the FreePascal Reference guide | |
| 161 | + | - [[Operator overloading|operator overloading]] (especially the group of assignment operators, and § “routing”) | |
| 162 | + | ||
| 163 | + | --- | |
| 164 | + | *From the Free Pascal wiki, page "Typecast", as it stood on 2023-10-18 after a change by FPC user, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
