Name

Type Keyword

Syntax

type Name = Type declaration; ...
type Name = type Type declaration;

type Name1 = Name2;
type Name = (Identifier, ...);
type Name = Expr1..Expr2;
type Name = ^Type;
type Name = array[...] of Name;
type Name = class ... end;
type Name = class of ...;
type Name = dispinterface ... end;
type Name = file of Type;
type Name = function ...;
type Name = interface ... end;
type Name = object ... end;
type Name = procedure ...;
type Name = record ... end;
type Name = set of Ordinal type;

Description

The type keyword begins a type declaration, as it does in standard Pascal.

If the type declaration begins with another occurrence of the type keyword, Delphi generates unique RTTI for the type, even if the type is just a synonym for an existing type. It also makes the type a distinct type with regard to var parameters.

Tips and Tricks

  • A common convention in Delphi programs is to begin type names with the letter T, except for exception classes (which begin with E), interfaces (which begin with I), and pointer types (which begin with P).

  • A forward class declaration must be resolved in the same type block where it is declared. For example:

type
  TExample = class; // forward declaration
  TOther = class
    procedure Example(E: TExample);
  end;
  // Full class declaration without using another "type" keyword,
  // which starts a new type block.
  TExample = class
    procedure Example(Other: TOther);
  end;
  • A class declaration in the interface section of a unit must have definitions for all of the class’s non-abstract methods in the implementation section of the same unit.

Example

type
  TSuit = (Diamond, Club, Heart, Spade);
  PCard = ^TCard;  // pointer to TCard;
  TCard = record
    Suit: TSuit;
    Rank: 1..13;
  end;
  Deck = array[1..52] of TCard;
  TSuits = set of TSuit;

  TCardFunc = function(const Card: TCard): Integer;
  TCardProc = procedure(const Card: TCard);
  TCardMethodFunc = function(const Card: TCard): Integer of object;
  TCardMethodProc = procedure(var Card: TCard) of object;

  IGame = interface
  ['{6164F471-5E41-11D3-B1B6-00105AA9C2AD}']
    procedure Play;
    procedure Draw(var Card: TCard);
  end;
  TGame = class(TInterfacedObject, IGame)
  public
    procedure Play;
    procedure Draw(var Card: TCard);
  end;

See Also

Array Keyword, Class Keyword, Dispinterface Keyword, File Keyword, Function Keyword, Integer Type, Interface Keyword, Object Keyword, Packed Keyword, Procedure Keyword, Record Keyword, Set Keyword, TClass Type, TextFile Type, TObject Type, TypeInfo Function
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset