Sign in Join
Wiki copytemporary

Revision 3 of 4, by WikiArchive on 11-4-2026. The page as it is now.

Default

Default is

This article describes the compiler intrinsic. See other articles for the notion in OOP.

Contents

zero-idiom

Since FPC 3.0.0 default(dataType) returns a zero value for the specified dataType.

pascal
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

pascal
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.

caveats

pascal
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.

see also


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.