Data Types, Variables, and Constants
This topic presents a high-level overview of Delphi data types.
About Types
A type is essentially a name for a kind of data. When you declare a variable you must specify its type, which
determines the set of values the variable can hold and the operations that can be performed on it. Every expression
returns data of a particular type, as does every function. Most functions and procedures require parameters of specific
types.
The Delphi language is a 'strongly typed'
language, which means that it distinguishes a variety of data types and
does not always allow you to substitute one type for another. This is usually beneficial because it lets the compiler
treat data intelligently and validate your code more thoroughly, preventing hard-to-diagnose runtime errors. When
you need greater flexibility, however, there are mechanisms to circumvent strong typing. These include typecasting,
pointers, Variants,
Variant parts in records, and absolute addressing of variables.
There are several ways to categorize Delphi data types:
Some types are predefined (or built-in); the compiler recognizes these automatically, without the need for a
declaration. Almost all of the types documented in this language reference are predefined. Other types are
created by declaration; these include user-defined types and the types defined in the product libraries.
Types can be classified as either fundamental or generic. The range and format
of a fundamental type is the
same in all implementations of the Delphi language, regardless of the underlying CPU and operating system.
The range and format of a generic type is platform-specific and could vary across different implementations.
Most predefined types are fundamental, but a handful of integer, character, string, and pointer types are generic.
It's a good idea to use generic types when possible, since they provide optimal performance and portability.
However, changes in storage format from one implementation of a generic type to the next could cause
compatibility problems - for example, if you are streaming content to a file as raw, binary data, without type and
versioning information.
Types can be classified as simple, string, structured, pointer, procedural, or variant. In addition, type identifiers
themselves can be regarded as belonging to a special 'type' because they can be passed as parameters to
certain functions (such as High, Low, and SizeOf).
The outline below shows the taxonomy of Delphi data types.
60
simple
ordinal
integer
character
Boolean
enumerated
subrange
real
string
structured
set
array
record
file
class
class reference
interface
pointer
procedural
Variant
type identifier
The standard function
SizeOf
operates on all variables and type identifiers. It returns an integer representing the
amount of memory (in bytes) required to store data of the specified type. For example,
SizeOf(Longint)
returns
4, since a Longint variable uses four bytes of memory.
Type declarations are illustrated in the topics that follow. For general information
about type declarations, see
Declaring types.
61