Difference between revisions of "Default"
From Free Pascal wiki
Jump to navigationJump to search (Created page with "'''Default''' is a compiler intrinsic: it returns for every type T a default value. In essence, this is a block of memory that is zeroed out. It can be used to correctly initi...") |
(expand) |
||
Line 1: | Line 1: | ||
− | '''Default''' is a compiler intrinsic | + | '''<syntaxhighlight lang="delphi" inline>Default</syntaxhighlight>''' is |
+ | * a compiler intrinsic [[Function|<syntaxhighlight lang="pascal" inline>function</syntaxhighlight>]] returning a zeroed out value, or | ||
+ | * a [[Keyword|keyword]] marking a [[Class|<syntaxhighlight lang="delphi" inline>class</syntaxhighlight>]]’s [[Array|<syntaxhighlight lang="pascal" inline>array</syntaxhighlight>]] [[Property|property]] as implicitly accessible. | ||
+ | This article describes the compiler intrinsic. | ||
+ | See other articles for the notion in OOP. | ||
− | + | == zero-idiom == | |
+ | [[FPC New Features 3.0.0#New compiler intrinsic Default|Since FPC 3.0.0]] <syntaxhighlight lang="delphi" inline>default(dataType)</syntaxhighlight> returns a zero value for the specified [[Data type|<syntaxhighlight lang="delphi" inline>dataType</syntaxhighlight>]]. | ||
+ | <syntaxhighlight lang="delphi">program defaults(input, output, stdErr); | ||
+ | var | ||
+ | i: integer; | ||
+ | s: string; | ||
+ | r: tOpaqueData; | ||
+ | begin | ||
+ | i := default(integer); { assigns `0` } | ||
+ | s := default(string); { assigns `nil` or `''` (empty string) } | ||
+ | r := default(tOpaqueData); { assigns `tOpaqueData[]` } | ||
+ | end.</syntaxhighlight> | ||
− | <syntaxhighlight lang=" | + | === advantages === |
− | + | * The most important “advantage” is that <syntaxhighlight lang="delphi" inline>default</syntaxhighlight> can be used in <syntaxhighlight lang="delphi" inline>generic</syntaxhighlight> definitions with template parameters.<syntaxhighlight lang="delphi" highlight="11">program defaultDemo(input, output, stdErr); | |
− | </syntaxhighlight> | + | {$mode objFPC} |
+ | { --- generic thing -------------------------------------- } | ||
+ | type | ||
+ | generic thing<storageType> = object | ||
+ | data: storageType; | ||
+ | constructor init; | ||
+ | end; | ||
+ | constructor thing.init; | ||
+ | begin | ||
+ | data := default(storageType); | ||
+ | end; | ||
+ | |||
+ | { === MAIN =============================================== } | ||
+ | type | ||
+ | arr = array[1..10] of integer; | ||
+ | var | ||
+ | x: specialize thing<tBoundArray>; | ||
+ | y: specialize thing<arr>; | ||
+ | begin | ||
+ | end.</syntaxhighlight>In the context of the definition of the <syntaxhighlight lang="delphi" inline>generic</syntaxhighlight> data type <syntaxhighlight lang="delphi" inline>thing</syntaxhighlight> it is not yet known what <syntaxhighlight lang="delphi" inline>storageType</syntaxhighlight> will be. Therefore, we could not possibly write a literal value such as <syntaxhighlight lang="delphi" inline>0</syntaxhighlight> or [[Nil|<syntaxhighlight lang="delphi" inline>nil</syntaxhighlight>]]. However, we ''can'' use <syntaxhighlight lang="delphi" inline>default</syntaxhighlight> to overcome this hurdle. Upon specialization the correct zero value will be inserted. | ||
+ | * Furthermore, the compiler can choose a ''faster'' implementation than, for instance, a respective <syntaxhighlight lang="delphi" inline style="whitespace: nowrap;">fillChar(myVariable, sizeOf(myVariable), chr(0))</syntaxhighlight>. | ||
+ | |||
+ | === caveats === | ||
+ | * Despite its name, <syntaxhighlight lang="delphi" inline>default</syntaxhighlight> is really just a synonym for zero. It ''can'' be used to [[Becomes|assign]] values ''out of range'':<syntaxhighlight lang="delphi" highlight="10">program faultyDefault(input, output, stdErr); | ||
+ | {$rangeChecks on} | ||
+ | type | ||
+ | typeWithoutZeroValue = -1337..-42; | ||
+ | var | ||
+ | i: typeWithoutZeroValue; | ||
+ | begin | ||
+ | i := -1024; { ✔ OK } | ||
+ | {i := 0; ✘ not OK } | ||
+ | i := default(typeWithoutZeroValue); { OK again } | ||
+ | writeLn(i); | ||
+ | end.</syntaxhighlight>See {{gitlab|issue|FPC|34972}}. | ||
+ | * <syntaxhighlight lang="delphi" inline>Default</syntaxhighlight> cannot be applied on [[File|<syntaxhighlight lang="pascal" inline>file</syntaxhighlight>]] or [[Text|<syntaxhighlight lang="pascal" inline>text</syntaxhighlight>]] data types and structured data types containing such. | ||
+ | |||
+ | == see also == | ||
+ | * [[management operators]] | ||
+ | |||
+ | [[Category: Code]] | ||
+ | [[Category: FPC]] |
Latest revision as of 12:37, 1 April 2022
Default
is
- a compiler intrinsic
function
returning a zeroed out value, or - a keyword marking a
class
’sarray
property as implicitly accessible.
This article describes the compiler intrinsic. See other articles for the notion in OOP.
zero-idiom
Since FPC 3.0.0 default(dataType)
returns a zero value for the specified dataType
.
program defaults(input, output, stdErr);
var
i: integer;
s: string;
r: tOpaqueData;
begin
i := default(integer); { assigns `0` }
s := default(string); { assigns `nil` or `''` (empty string) }
r := default(tOpaqueData); { assigns `tOpaqueData[]` }
end.
advantages
- The most important “advantage” is that
default
can be used ingeneric
definitions with template parameters.In the context of the definition of theprogram defaultDemo(input, output, stdErr); {$mode objFPC} { --- generic thing -------------------------------------- } type generic thing<storageType> = object data: storageType; constructor init; end; constructor thing.init; begin data := default(storageType); end; { === MAIN =============================================== } type arr = array[1..10] of integer; var x: specialize thing<tBoundArray>; y: specialize thing<arr>; begin end.
generic
data typething
it is not yet known whatstorageType
will be. Therefore, we could not possibly write a literal value such as0
ornil
. However, we can usedefault
to overcome this hurdle. Upon specialization the correct zero value will be inserted. - Furthermore, the compiler can choose a faster implementation than, for instance, a respective
fillChar(myVariable, sizeOf(myVariable), chr(0))
.
caveats
- Despite its name,
default
is really just a synonym for zero. It can be used to assign values out of range:See FPC issue 34972.program faultyDefault(input, output, stdErr); {$rangeChecks on} type typeWithoutZeroValue = -1337..-42; var i: typeWithoutZeroValue; begin i := -1024; { ✔ OK } {i := 0; ✘ not OK } i := default(typeWithoutZeroValue); { OK again } writeLn(i); end.
Default
cannot be applied onfile
ortext
data types and structured data types containing such.