Difference between revisions of "Type"
From Free Pascal wiki
Jump to navigationJump to search (fill with content) |
|||
Line 1: | Line 1: | ||
{{Type}} | {{Type}} | ||
− | The | + | The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">type</syntaxhighlight> is used to: |
+ | * start sections for user defined types, and | ||
+ | * identify a new type instance when referring to another data type. | ||
+ | |||
+ | |||
+ | == custom type definitions == | ||
+ | <syntaxhighlight lang="pascal" enclose="none">type</syntaxhighlight> starts a section, where the programmer may associate identifiers with new data types, especially structured data types such as [[Record|records]]. | ||
+ | <syntaxhighlight lang="pascal" line highlight="3"> | ||
+ | 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. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == type aliases == | ||
+ | In a <syntaxhighlight lang="pascal" enclose="none">type</syntaxhighlight> section aliases to already [[Data type|existing]] or previously defined data types can be declared. | ||
+ | The following example utilizes [[Conditional compilation|conditional compilation]] to alias the largest available unsigned integer type as <syntaxhighlight lang="pascal" enclose="none">wholeNumber</syntaxhighlight> (note there is already {{Doc|package=RTL|unit=system|identifier=sizeuint|text=<syntaxhighlight lang="pascal" enclose="none">system.sizeUInt</syntaxhighlight>}} defined). | ||
+ | <syntaxhighlight lang="pascal" line highlight="3"> | ||
+ | program typeAliasDemo(input, output, stderr); | ||
+ | |||
+ | type | ||
+ | wholeNumber = | ||
+ | {$ifdef CPU64} | ||
+ | qword | ||
+ | {$else} | ||
+ | {$ifdef CPU32} | ||
+ | longword | ||
+ | {$else} | ||
+ | {$fatal natural number too small} | ||
+ | {$endif} | ||
+ | {$endif} | ||
+ | ; | ||
+ | |||
+ | begin | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == type clone == | ||
+ | In a <syntaxhighlight lang="pascal" enclose="none">type</syntaxhighlight> section a type identifier preceded by the word <syntaxhighlight lang="pascal" enclose="none">type</syntaxhighlight> actually clones the type, with its type information, but creating different types. | ||
+ | <syntaxhighlight lang="pascal" line highlight="3,4"> | ||
+ | program typeCloneDemo(input, output, stderr); | ||
+ | |||
+ | type | ||
+ | wholeNumber = type qword; | ||
+ | |||
+ | begin | ||
+ | writeLn('qword: ', sysBackTraceStr(typeInfo(qword))); | ||
+ | writeLn('wholeNumber: ', sysBackTraceStr(typeInfo(wholeNumber))); | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == see also == | ||
+ | * [https://www.freepascal.org/docs-html/ref/refse19.html § “Type Aliases” in the Reference Guide] |
Revision as of 00:02, 2 June 2018
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
The reserved word type
is used to:
- start sections for user defined types, and
- identify a new type instance when referring to another data type.
custom type definitions
type
starts a section, where the programmer may associate identifiers with new data types, especially structured data types such as records.
1program typeDemo(input, output, stderr);
2
3type
4 atom = record
5 electrons: longword;
6 neutrons: longword;
7 protons: longword;
8 end;
9
10var
11 x: atom;
12
13begin
14 x.protons := 1; // H
15 x.neutrons := 1; // D
16 x.electrons := 1; // 0
17end.
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.sizeUInt
defined).
1program typeAliasDemo(input, output, stderr);
2
3type
4 wholeNumber =
5 {$ifdef CPU64}
6 qword
7 {$else}
8 {$ifdef CPU32}
9 longword
10 {$else}
11 {$fatal natural number too small}
12 {$endif}
13 {$endif}
14 ;
15
16begin
17end.
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.
1program typeCloneDemo(input, output, stderr);
2
3type
4 wholeNumber = type qword;
5
6begin
7 writeLn('qword: ', sysBackTraceStr(typeInfo(qword)));
8 writeLn('wholeNumber: ', sysBackTraceStr(typeInfo(wholeNumber)));
9end.