Velo by Example: Dictionaries

Dictionaries (dict[K:V]) are key-value collections with typed keys and values.

Create a dictionary with initial key-value pairs.

dict[int:str] d = new dict[int:str]{
    1: "one",
    2: "two",
    3: "three"
};

Access values by key. Add or change entries with bracket syntax.

str value = d[1];     # "one"
d[4] = "four";        # add new entry

Use set, del, key, and val methods for common operations.

d.set(5, "five");
bool deleted = d.del(2);    # true
bool exists = d.key(5);     # true
bool hasVal = d.val("one");  # true

len gives the count. keys and vals return arrays of keys and values.

int count = d.len;
array[int] allKeys = d.keys;
array[str] allValues = d.vals;