Sign in Join
Wiki copytemporary

Default: what changed

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

OldNewLine
1-**`Default`** is
1+**`Default`** is
22
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.
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.
55
6-This article describes the compiler intrinsic. See other articles for the notion in OOP.
6+This article describes the compiler intrinsic. See other articles for the notion in OOP.
77
8-## zero-idiom
8+## Contents
99
10-[[FPC New Features 3.0.0|Since FPC 3.0.0]] `default(dataType)` returns a zero value for the specified [[Data type|dataType]].
10+ * 1 zero-idiom
11+ * 1.1 advantages
12+ * 1.2 caveats
13+ * 2 see also
1114
12-```delphi
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+```
1319program 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.
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.
2329```
2430
2531### advantages
2632
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-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.
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+```
5457
55-- Furthermore, the compiler can choose a *faster* implementation than, for instance, a respective `fillChar(myVariable, sizeOf(myVariable), chr(0))`.
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))`.
5660
5761### caveats
5862
59-- Despite its name, `default` is really just a synonym for zero. It *can* be used to [[Becomes|assign]] values *out of range*:
60-
61-{\$rangeChecks on} type
62-
63-`   typeWithoutZeroValue = -1337..-42;`
64-
65-var
66-
67-`   i: typeWithoutZeroValue;`
68-
69-begin
70-
71-`   i := -1024; { ✔ OK }`\
72-`   {i := 0; ✘ not OK }`\
73-`   i := default(typeWithoutZeroValue); { OK again }`\
74-`   writeLn(i);`
75-
76-end.
77-
78-See [FPC issue 34972](https://gitlab.com/freepascal.org/fpc/source/-/issues/34972).
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+```
7978
80-- `Default` cannot be applied on [[File|file]] or [[Text|text]] data types and structured data types containing such.
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.
8181
8282## see also
8383
84-- [[management operators]]
84+ * [[management operators]]
8585
8686---
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).*
88-Category: Code
89-Category: FPC
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).*

Back to history