Sign in Join
Wiki copytemporary

Default: what changed

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

OldNewLine
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.
1+**`Default`** is
72
8-## Contents
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.
95
10- * 1 zero-idiom
11- * 1.1 advantages
12- * 1.2 caveats
13- * 2 see also
6+This article describes the compiler intrinsic. See other articles for the notion in OOP.
147
158## zero-idiom
169
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-```
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
1913program defaults(input, output, stdErr);
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.
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.
2923```
3024
3125### advantages
3226
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-```
27+- The most important “advantage” is that `default` can be used in `generic` definitions with template parameters.
5728
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))`.
29+{\$mode objFPC} { --- generic thing -------------------------------------- } type
6030
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.
54+
55+- Furthermore, the compiler can choose a *faster* implementation than, for instance, a respective `fillChar(myVariable, sizeOf(myVariable), chr(0))`.
56+
6157### caveats
6258
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-```
59+- Despite its name, `default` is really just a synonym for zero. It *can* be used to [[Becomes|assign]] values *out of range*:
7860
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.
61+{\$rangeChecks on} type
8162
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).
79+
80+- `Default` cannot be applied on [[File|file]] or [[Text|text]] data types and structured data types containing such.
81+
8282## see also
8383
84- * [[management operators]]
84+- [[management operators]]
8585
8686---
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).*
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

Back to history