Here is the definition of our EmployeeMap once more:

dictionary<int, Employee> EmployeeMap;

In the JavaScript mapping, dictionaries using a JavaScript built-in type as the key type are mapped to the JavaScript Map type. This is true for all Slice built-in types except long:

let em = new Map();

let e = new Employee();
e.number = 31;
e.firstName = "James";
e.lastName = "Gosling";

em.set(e.number, e);

For cases where the key type does not correspond with a JavaScript built-in type, the dictionary is mapped to HashMap. This is true for Slice dictionaries where the key type is long or a Slice structure that qualifies as a legal dictionary key:

dictionary<long, Employee> EmployeeMap;

In these cases an extra constructor is generated that initializes the HashMap with the desired comparison operator.

let em = new EmployeeMap();

let e = new Employee();
e.number = new Ice.Long(31);
e.firstName = "James";
e.lastName = "Gosling";

em.set(e.number, e);


HashMap supports the same API as the standard JavaScript Map object. It provides the following additional properties and functions:

Legal key types for HashMap include JavaScript's primitive types along with nullNaN, and any object that defines a hashCode method. The generated code for a Slice structure that qualifies as a legal dictionary key type includes a hashCode method. Suppose we define another dictionary type:

dictionary<Employee, string> EmployeeDeptMap;

The Slice compiler generates a constructor function equivalent to the following code:

class EmployeeDeptMap extends Ice.HashMap
{
    constructor(h)
    {
        let keyComparator = ...;
        let valueComparator = ...;
        super(h || keyComparator, valueComparator);
    }
}

Since the key is a user-defined structure type, the map requires a comparator that properly compares keys. Instantiating a map using new EmployeeDeptMap automatically sets the comparators, whereas calling new Ice.HashMap in this case would require you to supply your own comparators.

Slice dictionaries that map to a HashMap must be instantiated using the generated constructor.

Notes


See Also