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: 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 initialize any type, and more importantly, a managed type. It also works using a generic type template.
+
'''<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.
  
This function cannot be used on any of the file types or complex types that contain a file type.  
+
== 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="pascal">
+
=== advantages ===
function Default(T: AnyType): AnyType;
+
* 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

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 in generic definitions with template parameters.
    program 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.
    
    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. However, we can use default 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:
    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.
    
    See FPC issue 34972.
  • Default cannot be applied on file or text data types and structured data types containing such.

see also