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