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