Understanding how to organize your code into units and use them is fundamental to building scalable applications in DWScript.
DWScript code can be organized as standalone scripts or reusable units.
A unit consists of an interface section (public API) and an implementation section (internal logic).
unit MyMath;
interface
// Public declarations
function Add(a, b : Integer) : Integer;
implementation
// Internal implementation
function Add(a, b : Integer) : Integer;
begin
Result := a + b;
end;
end. Units can optionally include initialization and finalization blocks:
To use code from another unit, you must include it in the uses clause. This can appear in the main script or within a unit's interface/implementation sections.
program MyApp;
// Import the 'MyMath' unit
uses MyMath;
var sum := Add(10, 20);
PrintLn(sum); While DWScript supports dotted unit names for organization, it also features a unique unit namespace construct. This allows you to aggregate multiple existing units into a single identifier, making it easier to manage large libraries.
A namespace unit uses the unit namespace keywords and a uses clause to list the units it aggregates.
unit namespace MyLibrary;
uses
MyLibrary.Core,
MyLibrary.Utils; When a script uses the namespace unit, all public symbols from the aggregated units become available directly.
uses MyLibrary;
// Symbols from Core and Utils are now available
// without needing to import them individually.
var result := Process(FetchData);