next up previous contents index search.gif
Next: 3.2 Character types Up: 3. Types Previous: 3. Types

Subsections


3.1 Base types

The base or simple types of Free Pascal are the Delphi types. We will discuss each separate.

Simple types

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{simple\ type}
\(
\synt{ordinal\ type} \\
\synt{real\ type}
\)\end{syntdiag}

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{real\ type} \synt{real\ type\ identifier}\end{syntdiag}

3.1.1 Ordinal types

With the exception of Real types, all base types are ordinal types. Ordinal types have the following characteristics:
  1. Ordinal types are countable and ordered, i.e. it is, in principle, possible to start counting them one bye one, in a specified order. This property allows the operation of functions as Inc, Ord, Dec on ordinal types to be defined.
  2. Ordinal values have a smallest possible value. Trying to apply the Pred function on the smallest possible value will generate a range check error.
  3. Ordinal values have a largest possible value. Trying to apply the Succ function on the larglest possible value will generate a range check error.

3.1.1.1 Integers

A list of pre-defined ordinal types is presented in table (ordinals)

Table: Predefined ordinal types
Name
Integer
Shortint
SmallInt
Longint
Byte
Word
Cardinal
Boolean
ByteBool
LongBool
Char

The integer types, and their ranges and sizes, that are predefined in Free Pascal are listed in table (integers) .

Table: Predefined integer types
Type Range Size in bytes
Byte 0 .. 255 1
Shortint -127 .. 127 1
Integer -32768 .. 32767 23.1
Word 0 .. 65535 2
Longint -2147483648 .. 2147483648 4
Cardinal3.2 0..4294967296 4

Free Pascal does automatic type conversion in expressions where different kinds of integer types are used.

3.1.1.2 Boolean types

Free Pascal supports the Boolean type, with its two pre-defined possible values True and False, as well as the ByteBool, WordBool and LongBool. These are the only two values that can be assigned to a Boolean type. Of course, any expression that resolves to a boolean value, can also be assigned to a boolean type.

Table: Boolean types
Name Size Ord(True)
Boolean 1 1
ByteBool 1 Any nonzero value
WordBool 2 Any nonzero value
LongBool 4 Any nonzero value

Assuming B to be of type Boolean, the following are valid assignments:

 B := True;
 B := False;
 B := 1<>2;  { Results in B := True }
Boolean expressions are also used in conditions. Remark: In Free Pascal, boolean expressions are always evaluated in such a way that when the result is known, the rest of the expression will no longer be evaluated (Called short-cut evaluation). In the following example, the function Func will never be called, which may have strange side-effects.

 ...
 B := False;
 A := B and Func;
Here Func is a function which returns a Boolean type. Remark: The wordbool, longbool and bytebool were not supported by Free Pascal until version 0.99.6.

3.1.1.3 Enumeration types

Enumeration types are supported in Free Pascal. On top of the Turbo Pascal implementation, Free Pascal allows also a C-style extension of the enumeration type, where a value is assigned to a particular element of the enumeration list.

Enumerated types

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{enumerated\ ...
...ist} \\
\synt{assigned\ enum\ list}
\)\\
\lit*,
\end{rep} \lit )\end{syntdiag}

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{identifier\ list}
\begin{rep}[b]
\synt{identifier} \\
\lit*,
\end{rep}\end{syntdiag}

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{assigned\ en...
...\synt{identifier} \lit*{:=} \synt{expression} \\
\lit*,
\end{rep}\end{syntdiag}
(see chapter Expressions for how to use expressions) When using assigned enumerated types, the assigned elements must be in ascending numerical order in the list, or the compiler will complain. The expressions used in assigned enumerated elements must be known at compile time. So the following is a correct enumerated type declaration:

Type
  Direction = ( North, East, South, West );
The C style enumeration type looks as follows:

Type
  EnumType = (one, two, three, forty := 40,fortyone);
As a result, the ordinal number of forty is 40, and not 3, as it would be when the ':= 40' wasn't present. The ordinal value of fortyone is then 41, and not 4, as it would be when the assignment wasn't present. After an assignment in an enumerated definition the compiler adds 1 to the assigned value to assign to the next enumerated value. When specifying such an enumeration type, it is important to keep in mind that you should keep the enumerated elements in ascending order. The following will produce a compiler error:

Type
  EnumType = (one, two, three, forty := 40, thirty := 30);
It is necessary to keep forty and thirty in the correct order. When using enumeration types it is important to keep the following points in mind:
  1. You cannot use the Pred and Succ functions on this kind of enumeration types. If you try to do that, you'll get a compiler error.
  2. Enumeration types are by default stored in 4 bytes. You can change this behaviour with the {$PACKENUM n} compiler directive, which tells the compiler the minimal number of bytes to be used for enumeration types. For instance
    
    Type
      LargeEnum = ( BigOne, BigTwo, BigThree );
    {$PACKENUM 1}
      SmallEnum = ( one, two, three );
    Var S : SmallEnum;
        L : LargeEnum;
    begin
      WriteLn ('Small enum : ',SizeOf(S));
      WriteLn ('Large enum : ',SizeOf(L));
    end.
    
    will, when run, print the following:
    
    Small enum : 1
    Large enum : 4
    
More information can be found in the Programmers' guide, in the compiler directives section.

3.1.1.4 Subrange types

A subrange type is a range of values from an ordinal type (the host type). To define a subrange type, one must specify it's limiting values: the highest and lowest value of the type.

Subrange types

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{subrange\ type} \synt{constant} \lit*{..} \synt{constant}\end{syntdiag}
Some of the predefined integer types are defined as subrange types:

Type
  Longint  = $80000000..$7fffffff;
  Integer  = -32768..32767;
  shortint = -128..127;
  byte     = 0..255;
  Word     = 0..65535;
But you can also define subrange types of enumeration types:

Type
  Days = (monday,tuesday,wednesday, thursday,friday,
          saturday,sunday);
  WorkDays = monday .. friday;
  WeekEnd = Saturday .. Sunday;

3.1.2 Real types

Free Pascal uses the math coprocessor (or an emulation) for all its floating-point calculations. The Real native type is processor dependant, but it is either Single or Double. Only the IEEE floating point types are supported, and these depend on the target processor and emulation options. The true Turbo Pascal compatible types are listed in table (Reals) .

Table: Supported Real types
Type Range Significant digits Size3.3
Single 1.5E-45 .. 3.4E38 7-8 4
Real 5.0E-324 .. 1.7E308 15-16 8
Double 5.0E-324 .. 1.7E308 15-16 8
Extended 1.9E-4951 .. 1.1E4932 19-20 10
Comp3.4 -2E64+1 .. 2E63-1 19-20 8

Until version 0.9.1 of the compiler, all the Real types are mapped to type Double, meaning that they all have size 8. The SizeOf function is your friend here. The Real type of turbo pascal is automatically mapped to Double. The Comp type is, in effect, a 64-bit integer.

root
1999-06-10