default()

The default() function returns the default value of the specified type. That is, the value it would have if you didn't initialize it.

For reference types like Classes, the default value is nil/null; for records/structs, all fields will be initialized to their default values; for booleans this is false; and for integer, float and char types it will be 0 or #0'.

You should be careful when assuming default values for Enums. When no 0 value has been defined in the enum definition, the underlying default value is still 0, but accessing it can result in an exception.

type
  MyRecordType = record
  public
    MyField: Integer;
  end;
  MyEnumType1 = enum(One, Two, Three);
  MyEnumType2 = enum(None= 0, One, Two, Three);

...

var myInt    := default(Integer);      // 0
var myString := default(String)        // nil
var myRecord := default(MyRecordType); // myRecord.MyField is 0
var myEnum1  := default(MyEnumType1);  // invalid value, it is 0 which is not defined
var myEnum2  := default(MyEnumType2);  // None

method SomeClass.MyMethod<T>;
begin
  var myGeneric := default(T); // this is either nil, 0,
                               // or an empty record instance, depending on T
struct MyRecordType
{
    public int MyField;
}
enum MyEnumType1 { One, Two, Three }
enum MyEnumType2 { None= 0, One, Two, Three }

...

int myInt           = default(int);          // 0
string myString     = default(String)        // null
MyRecord myRecord   = default(MyRecordType); // myRecord.MyField is 0
MyEnumType1 myEnum1 = default(MyEnumType1);  // invalid value, it is 0 which is not defined
MyEnumType2 myEnum2 = default(MyEnumType2);  // None

...

void MyMethod<T>()
{
    T myGeneric = default(T); // this is either nil, 0,
                              // or an empty record instance, depending on T
    ...
struct MyRecordType {
    public MyField: Int
}
enum MyEnumType1 {
    case One, Two, Three
}
enum MyEnumType2 {
    case None= 0, One, Two, Three
}

...

let myInt    = default(Int)          // 0
let myString = default(String)       // nil
let myRecord = default(MyRecordType) // myRecord.MyField is 0
let myEnum1  = default(MyEnumType1)  // invalid value, it is 0 which is not defined
let myEnum2  = default(MyEnumType2)  // None

func MyMethod<T>() {
    let myGeneric = default(T); // this is either nil, 0,
                                // or an empty record instance, depending on T
    ...
struct MyRecordType {
    public int MyField;
}

enum MyEnumType1 { One, Two, Three }
enum MyEnumType2 { None= 0, One, Two, Three }

...

int myInt           = default(int);          // 0
String myString     = default(String)        // null
MyRecord myRecord   = default(MyRecordType); // myRecord.MyField is 0
MyEnumType1 myEnum1 = default(MyEnumType1);  // invalid value, it is 0 which is not defined
MyEnumType2 myEnum2 = default(MyEnumType2);  // None

...

void MyMethod<T>() {
    T myGeneric = default(T); // this is either nil, 0,
                              // or an empty record instance, depending on T
    ...

See Also