Default: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | **`Default`** is | |
| 2 | 2 | ||
| 3 | 3 | - a compiler intrinsic [[Function|function]] returning a zeroed out value, or | |
| 4 | 4 | - a [[Keyword|keyword]] marking a [[Class|class]]’s [[Array|array]] [[Property|property]] as implicitly accessible. | |
| 5 | 5 | ||
| 6 | 6 | This article describes the compiler intrinsic. See other articles for the notion in OOP. | |
| 7 | 7 | ||
| 8 | 8 | ## zero-idiom | |
| 9 | 9 | ||
| 10 | 10 | [[FPC New Features 3.0.0|Since FPC 3.0.0]] `default(dataType)` returns a zero value for the specified [[Data type|dataType]]. | |
| 11 | 11 | ||
| 12 | 12 | ```delphi | |
| 13 | 13 | program defaults(input, output, stdErr); | |
| 14 | 14 | var | |
| 15 | 15 | i: integer; | |
| 16 | 16 | s: string; | |
| 17 | 17 | r: tOpaqueData; | |
| 18 | 18 | begin | |
| 19 | 19 | i := default(integer); { assigns `0` } | |
| 20 | 20 | s := default(string); { assigns `nil` or `''` (empty string) } | |
| 21 | 21 | r := default(tOpaqueData); { assigns `tOpaqueData[]` } | |
| 22 | 22 | end. | |
| 23 | 23 | ``` | |
| 24 | 24 | ||
| 25 | 25 | ### advantages | |
| 26 | 26 | ||
| 27 | 27 | - The most important “advantage” is that `default` can be used in `generic` definitions with template parameters. | |
| 28 | 28 | ||
| 29 | 29 | {\$mode objFPC} { --- generic thing -------------------------------------- } type | |
| 30 | 30 | ||
| 31 | 31 | ` generic thing<storageType> = object`\ | |
| 32 | 32 | ` data: storageType;`\ | |
| 33 | 33 | ` constructor init;`\ | |
| 34 | 34 | ` end;` | |
| 35 | 35 | ||
| 36 | 36 | constructor thing.init; begin | |
| 37 | 37 | ||
| 38 | 38 | ` data := default(storageType);` | |
| 39 | 39 | ||
| 40 | 40 | end; | |
| 41 | 41 | ||
| 42 | 42 | { === MAIN =============================================== } type | |
| 43 | 43 | ||
| 44 | 44 | ` arr = array[1..10] of integer;` | |
| 45 | 45 | ||
| 46 | 46 | var | |
| 47 | 47 | ||
| 48 | 48 | ` x: specialize thing<tBoundArray>;`\ | |
| 49 | 49 | ` y: specialize thing<arr>;` | |
| 50 | 50 | ||
| 51 | 51 | begin end. | |
| 52 | 52 | ||
| 53 | - | ||
| 54 | - | ||
| 55 | 53 | In the context of the definition of the `generic` data type `thing` it is not yet known what `storageType` will be. Therefore, we could not possibly write a literal value such as `0` or [[Nil|nil]]. However, we *can* use `default` to overcome this hurdle. Upon specialization the correct zero value will be inserted. | |
| 56 | 54 | ||
| 57 | 55 | - Furthermore, the compiler can choose a *faster* implementation than, for instance, a respective `fillChar(myVariable, sizeOf(myVariable), chr(0))`. | |
| 58 | 56 | ||
| 59 | 57 | ### caveats | |
| 60 | 58 | ||
| 61 | 59 | - Despite its name, `default` is really just a synonym for zero. It *can* be used to [[Becomes|assign]] values *out of range*: | |
| 62 | 60 | ||
| 63 | 61 | {\$rangeChecks on} type | |
| 64 | 62 | ||
| 65 | 63 | ` typeWithoutZeroValue = -1337..-42;` | |
| 66 | 64 | ||
| 67 | 65 | var | |
| 68 | 66 | ||
| 69 | 67 | ` i: typeWithoutZeroValue;` | |
| 70 | 68 | ||
| 71 | 69 | begin | |
| 72 | 70 | ||
| 73 | 71 | ` i := -1024; { ✔ OK }`\ | |
| 74 | 72 | ` {i := 0; ✘ not OK }`\ | |
| 75 | 73 | ` i := default(typeWithoutZeroValue); { OK again }`\ | |
| 76 | 74 | ` writeLn(i);` | |
| 77 | 75 | ||
| 78 | 76 | end. | |
| 79 | - | ||
| 80 | - | ||
| 81 | 77 | ||
| 82 | 78 | See [FPC issue 34972](https://gitlab.com/freepascal.org/fpc/source/-/issues/34972). | |
| 83 | 79 | ||
| 84 | 80 | - `Default` cannot be applied on [[File|file]] or [[Text|text]] data types and structured data types containing such. | |
| 85 | 81 | ||
| 86 | 82 | ## see also | |
| 87 | 83 | ||
| 88 | 84 | - [[management operators]] | |
| 89 | 85 | ||
| 90 | 86 | --- | |
| 91 | 87 | *From the Free Pascal wiki, page "Default", as it stood on 2022-04-01 after a change by Kai Burghardt, 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).* | |
| 92 | 88 | Category: Code | |
| 93 | 89 | Category: FPC |
