Difference between revisions of "Type"

From Lazarus wiki
Jump to navigationJump to search
(→‎type clone: usage example)
(→‎type clone: Add info about assignment compatibility and extend code example)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Type}}
 
{{Type}}
  
 
+
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" inline>type</syntaxhighlight> is used to:
 
 
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">type</syntaxhighlight> is used to:
 
 
* start sections for user defined types, and
 
* start sections for user defined types, and
 
* identify a new type instance when referring to another [[Data type|data type]].
 
* identify a new type instance when referring to another [[Data type|data type]].
  
 
== custom type definitions ==
 
== 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" inline>type</syntaxhighlight> starts a section, where the programmer may associate [[Identifier|identifiers]] with new data types, especially structured data types such as [[Record|records]].
 
<syntaxhighlight lang="pascal" line highlight="3">
 
<syntaxhighlight lang="pascal" line highlight="3">
 
program typeDemo(input, output, stderr);
 
program typeDemo(input, output, stderr);
Line 30: Line 28:
  
 
== type aliases ==
 
== type aliases ==
In a <syntaxhighlight lang="pascal" enclose="none">type</syntaxhighlight> section aliases to already existing or previously defined data types can be declared.
+
In a <syntaxhighlight lang="pascal" inline>type</syntaxhighlight> section aliases to already 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=nativeuint|text=<syntaxhighlight lang="pascal" enclose="none">system.nativeUInt</syntaxhighlight>}} defined).
+
The following example utilizes [[Conditional compilation|conditional compilation]] to alias the largest available unsigned [[Integer|integer]] type as <syntaxhighlight lang="pascal" inline>wholeNumber</syntaxhighlight> (note there is already {{Doc|package=RTL|unit=system|identifier=nativeuint|text=<syntaxhighlight lang="pascal" inline>system.nativeUInt</syntaxhighlight>}} defined).
 
<syntaxhighlight lang="pascal" line highlight="3">
 
<syntaxhighlight lang="pascal" line highlight="3">
 
program typeAliasDemo(input, output, stderr);
 
program typeAliasDemo(input, output, stderr);
Line 53: Line 51:
  
 
== type clone ==
 
== 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.
+
In a <syntaxhighlight lang="pascal" inline>type</syntaxhighlight> section a type identifier preceded by the word <syntaxhighlight lang="pascal" inline>type</syntaxhighlight> 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).
<syntaxhighlight lang="pascal" line highlight="3,4">
+
 
 +
<syntaxhighlight lang="pascal" line highlight="3,4,15">
 +
 
 
program typeCloneDemo(input, output, stderr);
 
program typeCloneDemo(input, output, stderr);
  
 
type
 
type
wholeNumber = type qword;
+
  wholeNumber = type qword;
 +
 
 +
var
 +
  A: qword;
 +
  B: wholeNumber;
  
 
begin
 
begin
writeLn('qword:      ', sysBackTraceStr(typeInfo(qword)));
+
  writeLn('qword:      ', sysBackTraceStr(typeInfo(qword)));
writeLn('wholeNumber: ', sysBackTraceStr(typeInfo(wholeNumber)));
+
  writeLn('wholeNumber: ', sysBackTraceStr(typeInfo(wholeNumber)));
 +
 
 +
  A := 3;
 +
  B := A;
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
You want to do this, for instance in order to define a whole new set of operators.
+
 
Otherwise the operator definitions for type it was cloned from would still apply.
+
You want to do this, for instance in order to define a whole new set of [[Operator|operators]].
 +
Otherwise the [[Operator overloading|operator definitions]] for the type it was cloned from would still apply.
  
 
== see also ==
 
== see also ==
 
* [https://www.freepascal.org/docs-html/ref/refse19.html § “Type Aliases” in the Reference Guide]
 
* [https://www.freepascal.org/docs-html/ref/refse19.html § “Type Aliases” in the Reference Guide]

Latest revision as of 15:24, 29 July 2022

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.nativeUInt 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 whole 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. Nontheless, these types are still assigment compatible, which is a unique feature of the FPC (e. g. Delphi does not allow this assignment).

 1program typeCloneDemo(input, output, stderr);
 2
 3type
 4  wholeNumber = type qword;
 5
 6var
 7  A: qword;
 8  B: wholeNumber;
 9
10begin
11  writeLn('qword:       ', sysBackTraceStr(typeInfo(qword)));
12  writeLn('wholeNumber: ', sysBackTraceStr(typeInfo(wholeNumber)));
13
14  A := 3;
15  B := A;
16end.

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