Sign in Join
Wiki copytemporary

Default: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11**`Default`** is
22
33- a compiler intrinsic [[Function|function]] returning a zeroed out value, or
44- a [[Keyword|keyword]] marking a [[Class|class]]’s [[Array|array]] [[Property|property]] as implicitly accessible.
55
66This article describes the compiler intrinsic. See other articles for the notion in OOP.
77
88## zero-idiom
99
1010[[FPC New Features 3.0.0|Since FPC 3.0.0]] `default(dataType)` returns a zero value for the specified [[Data type|dataType]].
1111
1212```delphi
1313program defaults(input, output, stdErr);
1414var
1515 i: integer;
1616 s: string;
1717 r: tOpaqueData;
1818begin
1919 i := default(integer); { assigns `0` }
2020 s := default(string); { assigns `nil` or `''` (empty string) }
2121 r := default(tOpaqueData); { assigns `tOpaqueData[]` }
2222end.
2323```
2424
2525### advantages
2626
2727- The most important “advantage” is that `default` can be used in `generic` definitions with template parameters.
2828
2929{\$mode objFPC} { --- generic thing -------------------------------------- } type
3030
3131`   generic thing<storageType> = object`\
3232`           data: storageType;`\
3333`           constructor init;`\
3434`       end;`
3535
3636constructor thing.init; begin
3737
3838`   data := default(storageType);`
3939
4040end;
4141
4242{ === MAIN =============================================== } type
4343
4444`   arr = array[1..10] of integer;`
4545
4646var
4747
4848`   x: specialize thing<tBoundArray>;`\
4949`   y: specialize thing<arr>;`
5050
5151begin end.
5252
53-</syntaxhighlight>
54-
5553In 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.
5654
5755- Furthermore, the compiler can choose a *faster* implementation than, for instance, a respective `fillChar(myVariable, sizeOf(myVariable), chr(0))`.
5856
5957### caveats
6058
6159- Despite its name, `default` is really just a synonym for zero. It *can* be used to [[Becomes|assign]] values *out of range*:
6260
6361{\$rangeChecks on} type
6462
6563`   typeWithoutZeroValue = -1337..-42;`
6664
6765var
6866
6967`   i: typeWithoutZeroValue;`
7068
7169begin
7270
7371`   i := -1024; { ✔ OK }`\
7472`   {i := 0; ✘ not OK }`\
7573`   i := default(typeWithoutZeroValue); { OK again }`\
7674`   writeLn(i);`
7775
7876end.
79-
80-</syntaxhighlight>
8177
8278See [FPC issue 34972](https://gitlab.com/freepascal.org/fpc/source/-/issues/34972).
8379
8480- `Default` cannot be applied on [[File|file]] or [[Text|text]] data types and structured data types containing such.
8581
8682## see also
8783
8884- [[management operators]]
8985
9086---
9187*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).*
9288Category: Code
9389Category: FPC

Back to history