• Aucun résultat trouvé

Ordinal types

Dans le document Free Pascal : Reference guide. (Page 24-28)

2.3 Resource strings

3.1.1 Ordinal types

--- real type real type identifier

-3.1.1 Ordinal types

With the exception of int64, qword and 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 count-ing them one by one, in a specified order. This property allows the operation of func-tions asInc,Ord,Decon ordinal types to be defined.

2. Ordinal values have a smallest possible value. Trying to apply thePredfunction on the smallest possible value will generate a range check error if range checking is enabled.

3. Ordinal values have a largest possible value. Trying to apply theSuccfunction on the largest possible value will generate a range check error if range checking is enabled.

Integers

A list of pre-defined integer types is presented in table (3.1).

Table 3.1: Predefined integer types Name

Integer Shortint SmallInt Longint Longword Int64 Byte Word Cardinal QWord Boolean ByteBool WordBool LongBool Char

The integer types, and their ranges and sizes, that are predefined in Free Pascal are listed in table (3.2). Please note that theqwordandint64types are not true ordinals, so some Pascal constructs will not work with these two integer types.

Table 3.2: Predefined integer types

Type Range Size in bytes

Byte 0 .. 255 1

Shortint -128 .. 127 1

Smallint -32768 .. 32767 2

Word 0 .. 65535 2

Integer either smallint or longint size 2 or 4

Cardinal longword 4

Longint -2147483648 .. 2147483647 4

Longword 0 .. 4294967295 4

Int64 -9223372036854775808 .. 9223372036854775807 8

QWord 0 .. 18446744073709551615 8

Theintegertype maps to the smallint type in the default Free Pascal mode. It maps to either a longint in either Delphi or ObjFPC mode. Thecardinaltype is currently always mapped to the longword type.

Remark: All decimal constants which do no fit within the -2147483648..2147483647 range are silently and automatically parsed as 64-bit integer constants as of version 1.9.0. Earlier versions would convert it to a real-typed constant.

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

Boolean types

Free Pascal supports theBooleantype, with its two pre-defined possible valuesTrueand False. These are the only two values that can be assigned to aBooleantype. Of course, any expression that resolves to abooleanvalue, can also be assigned to a boolean type.

Free Pascal also supports theByteBool,WordBoolandLongBooltypes. These are of Table 3.3: Boolean types

Name Size Ord(True)

Boolean 1 1

ByteBool 1 Any nonzero value WordBool 2 Any nonzero value LongBool 4 Any nonzero value

typeByte,WordorLongint, but are assignment compatible with aBoolean: the value Falseis equivalent to 0 (zero) and any nonzero value is consideredTruewhen converting to a boolean value. A boolean value ofTrueis converted to -1 in case it is assigned to a variable of typeLongBool.

AssumingBto be of typeBoolean, 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 by default always evaluated in such a way that when the result is known, the rest of the expression will no longer be evaluated: this is called short-cut boolean evaluation.

In the following example, the functionFunc will never be called, which may have strange side-effects.

...

B := False;

A := B and Func;

HereFuncis a function which returns aBooleantype.

This behaviour is controllable by the{$B }compiler directive.

Enumeration types

Enumeration types are supported in Free Pascal. On top of the Turbo Pascal implementa-tion, 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

--- identifier list 6

identifier ,

--- assigned enum list 6

identifier := expression ,

-(see chapter 9, page 94for how to use expressions) When using assigned enumerated types, the assigned elements must be in ascending numerical order in the list, or the com-piler 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 );

A C-style enumeration type looks as follows:

Type

EnumType = (one, two, three, forty := 40,fortyone);

As a result, the ordinal number offorty is40, and not3, as it would be when the ’:=

40’wasn’t present. The ordinal value offortyoneis then 41, and not4, 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 the enu-merated elements should be kept in ascending order. The following will produce a compiler error:

Type

EnumType = (one, two, three, forty := 40, thirty := 30);

It is necessary to keepfortyandthirtyin the correct order. When using enumeration types it is important to keep the following points in mind:

1. The Pred and Succ functions cannot be used on this kind of enumeration types.

Trying to do this anyhow will result in a compiler error.

2. Enumeration types are stored using a default, independent of the actual number of values: the compiler does not try to optimize for space. This behaviour can be changed with the {$PACKENUM n} compiler directive, which tells the compiler the minimal number of bytes to be used for enumeration types. For instance

Type

{$PACKENUM 4}

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 theProgrammer’s Guide, in the compiler directives sec-tion.

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 its limiting values: the highest and lowest value of the type.

Subrange types

-- subrange type constant .. constant

-Some of the predefinedintegertypes are defined as subrange types:

Type

Longint = $80000000..$7fffffff;

Integer = -32768..32767;

shortint = -128..127;

byte = 0..255;

Word = 0..65535;

Subrange types of enumeration types can also be defined:

Type

Days = (monday,tuesday,wednesday,thursday,friday, saturday,sunday);

WorkDays = monday .. friday;

WeekEnd = Saturday .. Sunday;

Dans le document Free Pascal : Reference guide. (Page 24-28)

Documents relatifs