subrange types: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: subrange types
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Back to [[Data type|data types]]. | |
| 2 | + | ||
| 3 | + | Range of values: Corresponds to the definition defined by yourself | |
| 4 | + | ||
| 5 | + | Storage requirements: Corresponds to the underlying basic data type | |
| 6 | + | ||
| 7 | + | A **subrange type**: | |
| 8 | + | ||
| 9 | + | - is a subrange of a basic data type; | |
| 10 | + | - can only include values from the range of values that were defined when it was defined; | |
| 11 | + | ||
| 12 | + | Assigning other values leads to compiler error messages when the program is compiled and the compilation process is aborted. That is, the executable program is not created. | |
| 13 | + | ||
| 14 | + | There are two ways to define subrange types. | |
| 15 | + | ||
| 16 | + | Variant 1: | |
| 17 | + | ||
| 18 | + | ``` pascal | |
| 19 | + | var | |
| 20 | + | // Defines an integer subrange type | |
| 21 | + | // in the range of 10 to 123 | |
| 22 | + | tbNumber1 : 10 .. 123; | |
| 23 | + | ||
| 24 | + | // Defines a character subrange type | |
| 25 | + | // in the range of A to Z (all uppercase) | |
| 26 | + | tbLetter1 : 'A' .. 'Z'; | |
| 27 | + | ``` | |
| 28 | + | ||
| 29 | + | Variant 2: | |
| 30 | + | ||
| 31 | + | ``` pascal | |
| 32 | + | Type | |
| 33 | + | // Defines an integer subrange type | |
| 34 | + | // in the range of 10 to 123 | |
| 35 | + | TNumber2 = 10 .. 123; | |
| 36 | + | ||
| 37 | + | // Defines a character subrange type | |
| 38 | + | // in the range of A to Z (all uppercase) | |
| 39 | + | TLetter2 = 'A' .. 'Z'; | |
| 40 | + | ||
| 41 | + | var | |
| 42 | + | tbNumber2 : TNumber2 ; | |
| 43 | + | tbLetter2 : TLetter2 ; | |
| 44 | + | ``` | |
| 45 | + | ||
| 46 | + | Examples of assigning valid values: | |
| 47 | + | ||
| 48 | + | ``` pascal | |
| 49 | + | tbNumber1 := 10; | |
| 50 | + | tbNumber1 := 123; | |
| 51 | + | tbNumber2 := 10; | |
| 52 | + | tbNumber2 := 123; | |
| 53 | + | tbLetter1 := 'F'; | |
| 54 | + | tbLetter2 := 'F'; | |
| 55 | + | ``` | |
| 56 | + | ||
| 57 | + | Examples of assigning invalid values: | |
| 58 | + | ||
| 59 | + | ``` pascal | |
| 60 | + | // the values are outside the subrange | |
| 61 | + | tbNumber1 := 9; | |
| 62 | + | tbNumber2 := 124; | |
| 63 | + | tbLetter1 := 'f'; | |
| 64 | + | tbLetter2 := 'f'; | |
| 65 | + | // the values are string literals | |
| 66 | + | tbNumber1 := '10'; | |
| 67 | + | tbNumber1 := '123'; | |
| 68 | + | tbNumber2 := '10'; | |
| 69 | + | tbNumber2 := '123'; | |
| 70 | + | ``` | |
| 71 | + | ||
| 72 | + | --- | |
| 73 | + | *From the Free Pascal wiki, page "subrange types", as it stood on 2020-03-01 after a change by Trev, 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).* |
