Constants are values that are defined once and cannot be changed during the execution of the script. They are declared using the const keyword.
Constants must be assigned a value at the time of declaration. The compiler determines the type of the constant from the value assigned.
const PI = 3.14159;
const APP_NAME = 'My Script';
const MAX_RETRIES = 5;
const DEBUG_MODE = True;
PrintLn(APP_NAME); My Script
While DWScript primarily uses untracked constants (which are effectively replaced by their values at compile-time), it also supports typed constants which behave more like read-only variables.
const DEFAULT_TIMEOUT : Integer = 30; Constants can be defined using expressions, provided the expression can be evaluated at compile-time. This often involves using other previously defined constants.
const BASE_VALUE = 100;
const MULTIPLIER = 5;
const CALCULATED = BASE_VALUE * MULTIPLIER; // 500
const GREETING = 'Hello';
const USER = 'Admin';
const WELCOME_MSG = GREETING + ', ' + USER; // "Hello, Admin"
PrintLn(CALCULATED.ToString);
PrintLn(WELCOME_MSG); 500 Hello, Admin
DWScript provides several built-in constants and functions that behave like constants for common mathematical and system values.
Pi: The mathematical constant ΓΏ (3.14159...).True, False: Boolean literals.nil: The null pointer/object reference.PrintLn('The value of Pi is ' + FloatToStr(Pi)); The value of Pi is 3.14159265358979
:::tip
While you can define your own constant named PI, it is generally better to use the built-in Pi function for maximum precision.
:::
Like variables, constants can be declared at the global level or within the scope of a procedure or function.
const GLOBAL_VERSION = '1.0.0';
procedure ShowInfo;
const
LOCAL_PREFIX = 'VER: ';
begin
PrintLn(LOCAL_PREFIX + GLOBAL_VERSION);
end;
ShowInfo; VER: 1.0.0