Type: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Type
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The [[Reserved word|reserved word]] `type` is used to: | |
| 2 | + | ||
| 3 | + | - start sections for user defined types, and | |
| 4 | + | - identify a new type instance when referring to another [[Data type|data type]]. | |
| 5 | + | ||
| 6 | + | ## custom type definitions | |
| 7 | + | ||
| 8 | + | `type` starts a section, where the programmer may associate [[Identifier|identifiers]] with new data types, especially structured data types such as [[Record|records]]. | |
| 9 | + | ||
| 10 | + | ``` pascal | |
| 11 | + | program typeDemo(input, output, stderr); | |
| 12 | + | ||
| 13 | + | type | |
| 14 | + | atom = record | |
| 15 | + | electrons: longword; | |
| 16 | + | neutrons: longword; | |
| 17 | + | protons: longword; | |
| 18 | + | end; | |
| 19 | + | ||
| 20 | + | var | |
| 21 | + | x: atom; | |
| 22 | + | ||
| 23 | + | begin | |
| 24 | + | x.protons := 1; // H | |
| 25 | + | x.neutrons := 1; // D | |
| 26 | + | x.electrons := 1; // 0 | |
| 27 | + | end. | |
| 28 | + | ``` | |
| 29 | + | ||
| 30 | + | ## type aliases | |
| 31 | + | ||
| 32 | + | In a `type` 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|integer]] type as `wholeNumber` (note there is already [`system.nativeUInt`](https://www.freepascal.org/docs-html/rtl/system/nativeuint.html) defined). | |
| 33 | + | ||
| 34 | + | ``` pascal | |
| 35 | + | program typeAliasDemo(input, output, stderr); | |
| 36 | + | ||
| 37 | + | type | |
| 38 | + | wholeNumber = | |
| 39 | + | {$ifdef CPU64} | |
| 40 | + | qword | |
| 41 | + | {$else} | |
| 42 | + | {$ifdef CPU32} | |
| 43 | + | longword | |
| 44 | + | {$else} | |
| 45 | + | {$fatal whole number too small} | |
| 46 | + | {$endif} | |
| 47 | + | {$endif} | |
| 48 | + | ; | |
| 49 | + | ||
| 50 | + | begin | |
| 51 | + | end. | |
| 52 | + | ``` | |
| 53 | + | ||
| 54 | + | ## type clone | |
| 55 | + | ||
| 56 | + | 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). | |
| 57 | + | ||
| 58 | + | ``` pascal | |
| 59 | + | ||
| 60 | + | program typeCloneDemo(input, output, stderr); | |
| 61 | + | ||
| 62 | + | type | |
| 63 | + | wholeNumber = type qword; | |
| 64 | + | ||
| 65 | + | var | |
| 66 | + | A: qword; | |
| 67 | + | B: wholeNumber; | |
| 68 | + | ||
| 69 | + | begin | |
| 70 | + | writeLn('qword: ', sysBackTraceStr(typeInfo(qword))); | |
| 71 | + | writeLn('wholeNumber: ', sysBackTraceStr(typeInfo(wholeNumber))); | |
| 72 | + | ||
| 73 | + | A := 3; | |
| 74 | + | B := A; | |
| 75 | + | end. | |
| 76 | + | ``` | |
| 77 | + | ||
| 78 | + | 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. | |
| 79 | + | ||
| 80 | + | ## see also | |
| 81 | + | ||
| 82 | + | - [§ “Type Aliases” in the Reference Guide](https://www.freepascal.org/docs-html/ref/refse19.html) | |
| 83 | + | ||
| 84 | + | --- | |
| 85 | + | *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](/wiki-contact).* |
