Variables

Variables are storage locations with a specific type. DWScript supports both modern inline declarations and traditional Pascal-style blocks.

Explicit Declaration

You can specify the type explicitly using the name : Type syntax.

var x : Integer;
var name : String;

x := 10;
name := 'Alice';

Typed Initialization

You can combine declaration and assignment in a single statement.

var f : Float := 10;
var status : String := 'Initializing';

PrintLn(f);
Result
10

Type Inference

Modern DWScript can infer the type of a variable from its initial assignment. This is the preferred style for local variables as it reduces redundancy.

var count := 42;          // Inferred as Integer
var price := 19.99;       // Inferred as Float
var message := 'Hello';   // Inferred as String
var active := True;       // Inferred as Boolean

Declaration Styles

Modern (Inline)

In script mode, variables can be declared anywhere before their first use.

PrintLn('Step 1');
var x := 100;
PrintLn(x);
Result
Step 1
100

Classic Pascal Style

DWScript also supports the classic Pascal structure where variables are declared in a var block before the begin keyword of a procedure or function.

procedure ProcessData;
var
  i : Integer;
  s : String;
begin
  i := 5;
  s := 'Value: ';
  PrintLn(s + IntToStr(i));
end;

ProcessData;
Result
Value: 5

Reserved Names & Escaping

If you need to use a reserved Pascal keyword (like type, begin, end, repeat, etc.) as an identifier for a variable, field, or function, you can prefix it with an ampersand &.

The compiler will treat the identifier as if the ampersand were not there, allowing you to interface with external systems (like JSON APIs) that use these names.

var &type := 'System';
var &repeat := True;

PrintLn(&type);
Result
System

Scope & Lifetime

Variable visibility and lifetime are determined by where they are declared:

  • Global Scope: Variables declared at the top level of a script are visible everywhere and persist for the duration of the script execution.
  • Local Scope: Variables declared inside a procedure, function, or block (like a for loop) are only visible within that block. Their lifetime is tied to the procedure they are in.
  • Shadowing: A local variable can have the same name as a global one; in this case, the local variable "shadows" the global one within its scope.
var x := 'Global';

procedure Test;
var x := 'Local'; // Shadows global x
begin
  PrintLn(x);
end;

Test;
PrintLn(x);
Result
Local
Global

Variables in DWScript are guaranteed to be initialized to their default values (0 for numbers, '' for strings, nil for objects/arrays, False for booleans). You do not need to manually initialize them to zero/nil.

See Also

On this page