Metaprogramming

While DWScript is statically typed, it offers features that support dynamic behavior and metaprogramming styles.

Variant Types

The Variant type allows for late-bound method calls and property access. This is effectively dynamic typing within a static language.

var v := JSON.Parse('{"DynamicProp": 123}');
PrintLn(v.DynamicProp);
Result
123

JSON as Dynamic Objects

Since JSON objects in DWScript are fully dynamic, they can be used to construct data structures on the fly without defining strict classes.

Conditional Compilation

DWScript supports conditional compilation directives, allowing you to include or exclude code based on defined symbols.

{$DEFINE DEBUG}

{$IFDEF DEBUG}
  PrintLn('Debug mode enabled');
{$ELSE}
  PrintLn('Release mode');
{$ENDIF}
Result
Debug mode enabled
On this page