Name

$TypedAddress Compiler Directive

Syntax

{$T-}               // default
{$TypedAddress Off} // default
{$T+}
{$TypedAddress On}

Scope

Local

Description

When $TypedAddress is enabled, the @ operator returns a typed address of a variable or subroutine. The default is that the @ operator returns a generic Pointer type. Even when $TypedAddress is enabled, assignments of a typed pointer to the Pointer type are still allowed as are assignments from Pointer to a specific pointer type.

Tips and Tricks

  • Enable $TypedAddress because it encourages good programming practices and careful use of pointers. Unsafe pointer assignments can be caught at compiler time.

  • The Addr function always returns an untyped Pointer. If you enable $TypedAddress, use Addr when you need an untyped pointer and use @ when you want a type-safe address.

Example

var
  P: PInteger;
  Q: ^Double;
  I: Integer;
begin
{$TypedAddress On}
  P := @I;
  Q := @I; // not allowed because types don't match
{$TypedAddress Off}
  Q := @I; // allowed, even though types don't match

See Also

@ Operator, Addr Function, Pointer Type

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.