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
Excellent!
IMO this feature would benefit from a few additional methods in order serve (my needs at least) as a dictionary:
– The ability to remove an item from the dictionary. E.g. MyArray.Remove(Key).
– The ability to determine if the dictionary contains a given key. E.g. if (MyArray.Contains(Key)) then…
– The ability to iterate/enumerate the keys (or key/value pairs) of the dictionary. E.g. for var Key in MyArray do …
Additionally it would be nice if one could perform a reverse lookup (with the obvious limitations).
@SpeedFreak this is planned, alongside other variants of “classic” array methods like Map and operators ( in / not in, += )