Associate Arrays (aka dictionary) are now in experimental form in the DWScript repository.
They extend the array type declarations: in the same way you could declare an “array [TEnum] of Xxx” you can now declare an “array [String] of Xxx”, and not just with strings but with any type on either side.
The declaration syntax is
array [<some key type>] of <some element type>
The key type is matched on strict equality rules:
- for base types (Integer, String, Boolean, Float), same value
- for objects, same instance
- for dynamic arrays, same array reference
The basic usage is as follow:
var a : array [String] of Float; a['pi'] := 3.141592; // add or replace v := a['pi']; // read access
If a key is not present in an associative array, the value is that of the default for the type (zero for numeric, empty string, nil, etc.).
There are currently just two pseudo-methods supported:
- .Length will return the number of keys/elements in an array
- .Clear will empty an array