2957:New Language Features in Delphi 2.0
KEYWORDS: DELPHI 2.0 LANGUAGE DATA TYPES SYNTAX LANGUAGE NEW FEATURES AREA: Ob
New Language Features in Delphi 2.0 - 32
Delphi 2.0 defines several new data types that exploit the
features available by Windows 95. Delphi 2.0 has also changed
a few data types to take advantage of the 32-bit environment.
New data types include:
Character type
String type
Variant type
Currency type
Changed data types:
Integer
Cardinal
Additional Syntax:
Unit finalization section
New Data Types
--- ---- -----
Character Type
Delphi 2.0 introduces new wide character types to support
Unicode. Delphi 1.0 treated characters as 8-bit values of type
Char.
These are the standard types that represent characters in
Delphi 2.0.
ANSIChar - A standard 8-bit ANSI character, equivalent to the
Char type in previous versions of Delphi.
WideChar - A 16-bit character, representing a Unicode
character. If the high-order byte is zero, the
low-order byte contains an ANSI character.
Char - By default, Char is equivalent to ANSIChar. Char
works in the same way as the
implementation-dependent Integer type, which is
equivalent to SmallInt in 16-bit versions of Delphi
and to LongInt in 32-bit versions of Delphi. In
Delphi 2.0, Char defaults to an 8-bit value.
Character-pointer types:
Pointer type Character type
-----------------------------------
PANSIChar ANSIChar
PWideChar WideChar
PChar Char
The semantics of all the character-pointer types
are identical. The only thing that varies is the
size of the character pointed to.
String Type
Delphi 2.0 supports strings of nearly unlimited length in
addition to the 255-character counted strings previously
supported. A new compiler directive, $H, controls whether the
reserved word "string" represents a short string or the new,
long string. The default state of $H, is $H+, using long
strings by default. All Delphi 2.0 components use the new long
string type.
These are the new string types.
ShortString - A counted string with a maximum length of 255
characters. Equivalent to String in
Delphi 1.0. Each element is of type ANSIChar.
AnsiString - A new-style string of variable length, also
called a "long string." Each element is of
type ANSIChar.
string - Either a short string or an ANSI string,
depending on the value of the $H compiler
directive.
Here are the compatibility issues.
Although most string code works interchangeably between short
strings and long strings, there are certain short-string
operations that either won't work on long strings at all or
which operate more efficiently when done a different way. The
following table summarizes these changes.
Short String Long string
operation equivalent Explanation
----------------------------------------------------------------
PString type string All long strings are
dynamically allocated, so
PString is redundant and
requires more bookkeeping.
S[0] := L SetLength(S,L) Because long strings are
SetString(S,P,L) dynamically allocated, you
must call the SetLength
procedure to allocate the
appropriate amount of memory.
StrPCopy StrPCopy(Buffer,
(Buffer, S) PChar(S)) You can typecast long strings
into null-terminated strings.
The address of the long
string is the address of its
first character, and the long
string is followed by a null.
S := StrPas(P) S := P Long strings can automatically
copy from null-terminated
strings.
Long strings cannot be passed to OpenString-type parameters or
var short-string parameters.
Variant Type
Delphi 2.0 introduces variant types to give you the flexibility
to dynamically change the type of a variable. This is useful
when implementing OLE automation or certain kinds of database
operations where the parameter types on the server are unknown
to your Delphi-built client application.
A variant type is a 16-byte structure that has type
information embedded in it along with its value, which can
represent a string, integer, or floating-point value. The
compiler recognizes the standard type identifier Variant as the
declaration of a variant.
In cases where the type of a variant is incompatible with the
type needed to complete an operation, the variant will
automatically promote its value to a compatible value, if
possible. For instance, if a variant contains an integer and
you assign it to a string, the variant converts its value into
the string representing the integer number, which is then
assigned to the string.
You can also assign a variant expression to a variable of a
standard type or pass the variant as a parameter to a routine
that expects a standard type as a parameter. Delphi coerces the
variant value into a compatible type if necessary, and raises
an exception if it cannot create a compatible value.
Currency Type
Delphi 2.0 defines a new type called Currency, which is a
floating-point type specifically designed to handle large
values with great precision. Currency is assignment-compatible
with all other floating-point types (and variant types), but
is actually stored in a 64-bit integer value much like the Comp
type.
Currency-type values have a four-decimal-place precision. That
is, the floating-point value is stored in the integer format
with the four least significant digits implicitly representing
four decimal places.
Changed Data Types
------- ---- -----
The implementation-dependent types Integer and Cardinal are
32-bit values in Delphi 2.0, where they were 16-bit values in
Delphi 1.0. To explicitly declare 16-bit integer data types,
use the SmallInt and Word types.
Additional Syntax
---------- ------
You can include an optional finalization section in a unit.
Finalization is the counterpart of initialization, and takes
place when the application shuts down. You can think of the
finalization section as "exit code" for a unit. The
finalization section corresponds to calls to ExitProc and
AddExitProc in Delphi 1.0.
The finalization begins with the reserved word finalization.
The finalization section must appear after the initialization
section, but before the final end. statement.
Once execution enters an initialization section of a unit,
the corresponding finalization section is guaranteed to
execute when the application shuts down. Finalization sections
must handle partially-initialized data properly, just as class
destructors must.
Finalization sections execute in the opposite order that units
were initialized. For example, if your application initializes
units A, B, and C, in that order, it will finalize them in the
order C, B, and A.
The outline for a unit therefore looks like this:
unit UnitName;
interface
{ uses clause; optional }
...
implementation
{ uses clause; optional }
...
initialization { optional }
...
finalization { optional }
...
end.
TI