Records

Records are structured types that group related data together. In DWScript, they are powerful value types that support methods and operator overloading.

Basic Records

In its simplest form, a record is a set of data fields. It maps directly to a block of memory.

type
  TPoint = record
    X, Y: Integer;
  end;

var p: TPoint;
p.X := 10;
p.Y := 20;

Fields in a record are public by default.

Default Field Values

You can specify default values for fields in a record. These values are applied when the record is initialized. Type inference is also supported.

type
  TSettings = record
    Width: Integer := 800;
    Height := 600; // Type inferred as Integer
    Name: String := 'Untitled';
  end;

var s: TSettings;
PrintLn(s.Width);
PrintLn(s.Height);
PrintLn(s.Name);
Result
800
600
Untitled

Anonymous Records

Anonymous records allow you to define and initialize a record in a single expression, similar to JSON objects. They are fully compatible with System.JSON.

var user := record
  id := 123;
  name := 'Alice';
  &type := 'Admin';  // '&' allows using reserved words like 'type'
end;

PrintLn(user.name); 
PrintLn(user.&type);
Result
Alice
Admin

JSON Compatibility

Anonymous records are highly compatible with JSON.Stringify. In DWScript, anonymous records have their fields in a published section by default, ensuring they are automatically included during serialization.

The & prefix is also handled during serialization, so &type becomes "type" in the resulting JSON.

For more details, see JSON Support.

Advanced Features

Unlike standard Pascal records, DWScript records can have visibility specifiers, methods, and properties.

type
  TVec2 = record
  private
    FX, FY: Float;
  public
    procedure SetPos(x, y: Float);
    function Length: Float;
    property X: Float read FX;
    property Y: Float read FY;
  end;

procedure TVec2.SetPos(x, y: Float);
begin
  FX := x; FY := y;
end;

function TVec2.Length: Float;
begin
  Result := Sqrt(FX*FX + FY*FY);
end;

Value Semantics

Records are value types. This means they are copied when assigned to a new variable or passed to a function (unless using var). They do not support inheritance.

type
  TPoint = record
    X, Y: Integer;
  end;

var a, b: TPoint;
a.X := 10;
b := a; // b is a copy of a
b.X := 20; // a.X remains 10

Operator Overloading

You can define how operators like +, -, or = behave for your record types using global operators.

type
  TComplex = record
    Re, Im: Float;
  end;

function ComplexAdd(const a, b: TComplex): TComplex;
begin
  Result.Re := a.Re + b.Re;
  Result.Im := a.Im + b.Im;
end;

operator + (TComplex, TComplex): TComplex uses ComplexAdd;

var c1, c2, c3: TComplex;
c1.Re := 1; c1.Im := 2;
c2.Re := 3; c2.Im := 4;
c3 := c1 + c2;

PrintLn(c3.Re.ToString + ' + ' + c3.Im.ToString + 'i');
Result
4 + 6i

Implicit Operators

DWScript supports Implicit Operators, which allow for automatic type conversion between records and other types. This is useful for creating types that behave like primitives.

type
  TMoney = record
    Amount: Float;
    Currency: String;
  end;

function FloatToMoney(f: Float): TMoney;
begin
  Result.Amount := f;
  Result.Currency := 'USD';
end;

// Enable automatic conversion from Float to TMoney
operator implicit (Float): TMoney uses FloatToMoney;

var wallet: TMoney;
wallet := 50.0; // Automatically calls FloatToMoney

PrintLn(wallet.Amount.ToString + ' ' + wallet.Currency);
Result
50 USD
On this page