Dict
A dict is a key - value string pair storage. It can be constructed using the curly brace syntax:var data = { "hello" => "world" "lol" => "wtf" }Pairs can optionally be separated by a semicolon when you need to write the dict in one line (for example, when passing it to a function).
Keys can be defined by any of the following:
- string literals
- integers
- floats
- identifiers
- expressions in parens
Please note, that identifier keys are not resolved, and are rather used as "bare strings". Consider the following code snippet:
var foo = "bar" var dict1 = { foo => "hello" } // same as { "foo" => "hello" } var dict2 = { (foo) => "hello" } // interpreted as expression! { "bar" => "hello"}
Values can either be strings, or any other object for which a to_s convertor is defined.
It should also be noted that the dict type contains both keys and values as strings. Using ints, floats and identifiers as keys is merely a syntactic sugar, and values will need to be converted from strings to desired type manually.
Operators
The following operators can be applied:- Addition: create a mix of two dicts
- Subtraction: create a dict that contains all items from left dict except for items from right one
Methods
The type contains the following methods:- int size: gets the number of key-value pairs in the dict
- string get(string key): return a value by key
- void set(string key, string value): set a value
- void unset(string key): remove a given item
- bool has(string key): checks if the dict contains a key
- bool has_value(string value): checks if the dict contains a value
- string[] keys: gets an array of keys
- string[] values: gets an array of values