Typecast

From Lazarus wiki
Jump to navigationJump to search

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