Sign in Join
Wiki copytemporary

Type

Deutsch · English · Français · Suomi · Русский

The reserved word type is used to:

custom type definitions

type starts a section, where the programmer may associate identifiers with new data types, especially structured data types such as records.

pascal
program typeDemo(input, output, stderr);

type
    atom = record
        electrons: longword;
        neutrons: longword;
        protons: longword;
    end;

var
    x: atom;

begin
    x.protons := 1; // H
    x.neutrons := 1; // D
    x.electrons := 1; // 0
end.

type aliases

In a type section aliases to already existing or previously defined data types can be declared. The following example utilizes conditional compilation to alias the largest available unsigned integer type as wholeNumber (note there is already system.nativeUInt defined).

pascal
program typeAliasDemo(input, output, stderr);

type
    wholeNumber =
        {$ifdef CPU64}
            qword
        {$else}
            {$ifdef CPU32}
                longword
            {$else}
                {$fatal whole number too small}
            {$endif}
        {$endif}
        ;

begin
end.

type clone

In a type section a type identifier preceded by the word type actually clones the type, with its type information, but creating different types. Nontheless, these types are still assigment compatible, which is a unique feature of the FPC (e. g. Delphi does not allow this assignment).

pascal

program typeCloneDemo(input, output, stderr);

type
  wholeNumber = type qword;

var
  A: qword;
  B: wholeNumber;

begin
  writeLn('qword:       ', sysBackTraceStr(typeInfo(qword)));
  writeLn('wholeNumber: ', sysBackTraceStr(typeInfo(wholeNumber)));

  A := 3;
  B := A;
end.

You want to do this, for instance in order to define a whole new set of operators. Otherwise the operator definitions for the type it was cloned from would still apply.

see also


From the Free Pascal wiki, page "Type", as it stood on 2022-07-29 after a change by BrainChemistry, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.